简体   繁体   English

同步使用Alchemy API

[英]Using Alchemy API Synchronously

I am attempting to write a pure function to use the Alchemy API with the watson-developer-cloud npm package, but I cannot figure out how to execute its calls synchronously. 我正在尝试编写一个纯函数,以将Alchemy API与watson-developer-cloud npm软件包一起使用,但是我无法弄清楚如何同步执行其调用。 Is there an alternative method or package whereby I could receive its results synchronously? 是否有其他方法或软件包可以使我同步接收其结果? Blocking while the I/O is occurring is absolutely fine. 在发生I / O时阻塞绝对是可以的。

You can not do synchronous calls with the watson-developer-cloud npm module. 无法使用watson-developer-cloud npm模块进行同步调用。 What you can do is use Promises and mimic the synchronous model. 您可以做的是使用Promises并模仿同步模型。

The example below shows how to call the AlchemyVision recognizeFaces method using promises: 下面的例子说明如何调用AlchemyVision recognizeFaces使用承诺的方法:

var watson = require('watson-developer-cloud');
var Q = require('q');

var alchemy_vision = watson.alchemy_vision({
  api_key: '<api_key>'
});

// Creates a promise-returning function from a Node.js-style function
var recognizeFaces = Q.denodeify(alchemy_vision.recognizeFaces.bind(alchemy_vision));

var params = {
  url: 'http://si.wsj.net/public/resources/images/BN-BY925_mag041_OZ_20140318165119.jpg'
};

recognizeFaces(params).then(function (keywords) {
  console.log(JSON.stringify(keywords, null, 2));
}).catch(function (err) {
    console.log('error:', err);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM