我如何承诺 Watson Assistant 功能以允许节点中的 async/await?
How can I promisify Watson Assistant functions to allow async/await in node?
根据 Watson Assistant API 文档,回调用于返回响应。以下是取自他们的 API Documentation:
的示例
var watson = require('watson-developer-cloud');
var assistant = new watson.AssistantV1({
username: '{username}',
password: '{password}',
version: '2018-02-16'
});
var params = {
workspace_id: '9978a49e-ea89-4493-b33d-82298d3db20d',
intent: 'hello'
};
assistant.listExamples(params, function(err, response) {
if (err) {
console.error(err);
} else {
console.log(JSON.stringify(response, null, 2));
}
});
我希望尝试并承诺此功能,以便我可以将流程转换为一个漂亮、整洁的 async/await 流程并摆脱回调地狱。我在 AWS Lambda 上托管并执行此代码。他们最近在 Lambda 上发布了 node8.10 运行时,所以我很想使用 async/await.
转换我所有现有的函数
以下是我的尝试(替换了敏感数据):
var Watson = require('watson-developer-cloud');
var util = require('util');
var assistant = new Watson.AssistantV1({
username: "username",
password: "password",
version: "2018-02-16"
});
var params = {
workspace_id: "workspace_id",
intent: "acronym"
};
var watsonPromise = util.promisify(assistant.listExamples);
exports.handler = async (event) => {
try {
var examples = await watsonPromise(params);
return examples;
} catch (err) {
console.log(err);
return err;
}
}
这似乎不起作用,我收到以下错误:
START RequestId: 4f203ed1-4181-11e8-81ec-837163404af0 Version: $LATEST
2018-04-16T14:20:27.792Z 4f203ed1-4181-11e8-81ec-837163404af0 TypeError: Cannot read property '_options' of undefined
at AssistantV1.listExamples (/var/task/node_modules/watson-developer-cloud/assistant/v1.js:761:51)
at internal/util.js:230:26
at exports.handler (/var/task/index.js:21:30)
END RequestId: 4f203ed1-4181-11e8-81ec-837163404af0
经过一番挖掘,我的 examples
对象似乎显示为 undefined
。
谁能给点建议?不太确定我还能做什么。我可能缺少一些简单的东西。谢谢。
您可以将函数绑定到正确的上下文
var watsonPromise = util.promisify(assistant.listExamples.bind(assistant));
或者使用正确的上下文调用 promisified 版本
var examples = await watsonPromise.call(assisntant, params);
根据 Watson Assistant API 文档,回调用于返回响应。以下是取自他们的 API Documentation:
的示例var watson = require('watson-developer-cloud');
var assistant = new watson.AssistantV1({
username: '{username}',
password: '{password}',
version: '2018-02-16'
});
var params = {
workspace_id: '9978a49e-ea89-4493-b33d-82298d3db20d',
intent: 'hello'
};
assistant.listExamples(params, function(err, response) {
if (err) {
console.error(err);
} else {
console.log(JSON.stringify(response, null, 2));
}
});
我希望尝试并承诺此功能,以便我可以将流程转换为一个漂亮、整洁的 async/await 流程并摆脱回调地狱。我在 AWS Lambda 上托管并执行此代码。他们最近在 Lambda 上发布了 node8.10 运行时,所以我很想使用 async/await.
转换我所有现有的函数以下是我的尝试(替换了敏感数据):
var Watson = require('watson-developer-cloud');
var util = require('util');
var assistant = new Watson.AssistantV1({
username: "username",
password: "password",
version: "2018-02-16"
});
var params = {
workspace_id: "workspace_id",
intent: "acronym"
};
var watsonPromise = util.promisify(assistant.listExamples);
exports.handler = async (event) => {
try {
var examples = await watsonPromise(params);
return examples;
} catch (err) {
console.log(err);
return err;
}
}
这似乎不起作用,我收到以下错误:
START RequestId: 4f203ed1-4181-11e8-81ec-837163404af0 Version: $LATEST
2018-04-16T14:20:27.792Z 4f203ed1-4181-11e8-81ec-837163404af0 TypeError: Cannot read property '_options' of undefined
at AssistantV1.listExamples (/var/task/node_modules/watson-developer-cloud/assistant/v1.js:761:51)
at internal/util.js:230:26
at exports.handler (/var/task/index.js:21:30)
END RequestId: 4f203ed1-4181-11e8-81ec-837163404af0
经过一番挖掘,我的 examples
对象似乎显示为 undefined
。
谁能给点建议?不太确定我还能做什么。我可能缺少一些简单的东西。谢谢。
您可以将函数绑定到正确的上下文
var watsonPromise = util.promisify(assistant.listExamples.bind(assistant));
或者使用正确的上下文调用 promisified 版本
var examples = await watsonPromise.call(assisntant, params);