我是节点的新手,我正在尝试找出回调和它的异步性质。
我有这种功能:
myObj.exampleMethod = function(req, query, profile, next) {
// sequential instructions
var greetings = "hello";
var profile = {};
var options = {
headers: {
'Content-Type': 'application/json',
},
host: 'api.github.com',
path: '/user/profile'
json: true
};
// async block
https.get(options, function(res) {
res.on('data', function(d) {
// just an example
profile.emails = [
{value: d[0].email }
];
});
}).on('error', function(e) {
console.error(e);
});
//sync operations that needs the result of the https call above
profile['who'] = "that's me"
// I should't save the profile until the async block is done
save(profile);
}
鉴于大多数节点开发人员都使用这个或类似的解决方案,我还试图了解如何使用Async 库。
如何“阻止”(或等待结果)我的脚本流,直到我从 http 请求中获得结果?可能以异步库为例
谢谢