将 nexmo.message.sendSms 的回调转换为 async/await?
Convert the callback to async/await for nexmo.message.sendSms?
我的 nodejs 应用程序调用 nexmo
API 发送短信。这是 API:
nexmo.message.sendSms(sender, recipient, message, options, callback);
在应用程序中,它是
const nexmo = new Nexmo({
apiKey: "nexmoApiKey",
apiSecret: "nexmoSecret"
}, { debug: true });
nexmo.message.sendSms(nexmo_sender_number, cell_country + to, message, {type: 'unicode'}, async (err, result) => {....});
有什么方法可以将其转换为如下所示的 async/await 结构:
const {err, result} = nexmo.message.sendSms(nexmo_sender_number, cell_country + to, vcode, {type: 'unicode'});
if (err) {.....};
//then process result...
我想return消息发送成功后message
给父函数
nexmo-node
库目前仅支持回调。您需要使用 promisify
或 bluebird
之类的东西将 sendSms 函数转换为承诺,并使用 async/await 。这是一个使用 Node 的 promisify
的例子
const util = require('util');
const Nexmo = require('nexmo');
const nexmo = new Nexmo({
apiKey: "nexmoApiKey",
apiSecret: "nexmoSecret"
}, { debug: true });
const sendSms = util.promisify(nexmo.message.sendSms);
async function sendingSms() {
const {err, result} = await sendSms(nexmo_sender_number, cell_country + to, message, {type: 'unicode'});
if (err) {...} else {
// do something with result
}
}
尽管 Alex 的解决方案很优雅。它破坏了 TypeScript 并且 util 做了一些 'hidden logic' 的承诺;当它出错时,堆栈跟踪不清晰。
这还可以让您忠实于 API 并自动填充属性。
所以你可以这样做 (TypeScript)
/**
* Return the verification id needed.
*/
export const sendSMSCode = async (phoneNumber: string): Promise<string> => {
const result = await new Promise(async (resolve: (value: RequestResponse) => void, reject: (value: VerifyError) => void) => {
await nexmo.verify.request({
number: phoneNumber,
brand: `${Config.productName}`,
code_length: 4
}, (err, result) => {
console.log(err ? err : result)
if (err) {
reject(err)
}
resolve(result)
});
})
return result.request_id
}
我的 nodejs 应用程序调用 nexmo
API 发送短信。这是 API:
nexmo.message.sendSms(sender, recipient, message, options, callback);
在应用程序中,它是
const nexmo = new Nexmo({
apiKey: "nexmoApiKey",
apiSecret: "nexmoSecret"
}, { debug: true });
nexmo.message.sendSms(nexmo_sender_number, cell_country + to, message, {type: 'unicode'}, async (err, result) => {....});
有什么方法可以将其转换为如下所示的 async/await 结构:
const {err, result} = nexmo.message.sendSms(nexmo_sender_number, cell_country + to, vcode, {type: 'unicode'});
if (err) {.....};
//then process result...
我想return消息发送成功后message
给父函数
nexmo-node
库目前仅支持回调。您需要使用 promisify
或 bluebird
之类的东西将 sendSms 函数转换为承诺,并使用 async/await 。这是一个使用 Node 的 promisify
const util = require('util');
const Nexmo = require('nexmo');
const nexmo = new Nexmo({
apiKey: "nexmoApiKey",
apiSecret: "nexmoSecret"
}, { debug: true });
const sendSms = util.promisify(nexmo.message.sendSms);
async function sendingSms() {
const {err, result} = await sendSms(nexmo_sender_number, cell_country + to, message, {type: 'unicode'});
if (err) {...} else {
// do something with result
}
}
尽管 Alex 的解决方案很优雅。它破坏了 TypeScript 并且 util 做了一些 'hidden logic' 的承诺;当它出错时,堆栈跟踪不清晰。
这还可以让您忠实于 API 并自动填充属性。
所以你可以这样做 (TypeScript)
/**
* Return the verification id needed.
*/
export const sendSMSCode = async (phoneNumber: string): Promise<string> => {
const result = await new Promise(async (resolve: (value: RequestResponse) => void, reject: (value: VerifyError) => void) => {
await nexmo.verify.request({
number: phoneNumber,
brand: `${Config.productName}`,
code_length: 4
}, (err, result) => {
console.log(err ? err : result)
if (err) {
reject(err)
}
resolve(result)
});
})
return result.request_id
}