Chr*_*ris 246 javascript multithreading callback node.js
我有一个简化的功能,如下所示:
function(query) {
myApi.exec('SomeCommand', function(response) {
return response;
});
}
Run Code Online (Sandbox Code Playgroud)
基本上我希望它调用myApi.exec
,并返回回调lambda中给出的响应.但是,上面的代码不起作用,只是立即返回.
只是为了一个非常hackish尝试,我尝试了下面没有工作,但至少你明白了我想要实现的目标:
function(query) {
var r;
myApi.exec('SomeCommand', function(response) {
r = response;
});
while (!r) {}
return r;
}
Run Code Online (Sandbox Code Playgroud)
基本上,什么是一个好的'node.js /事件驱动'的方式来解决这个问题?我希望我的函数等到调用回调,然后返回传递给它的值.
Jak*_*kob 267
"good node.js/event driven"这样做的方法就是不要等待.
与使用像节点这样的事件驱动系统几乎所有其他内容一样,您的函数应该接受一个回调参数,该参数将在计算完成时调用.调用者不应该等待正常意义上的"返回"值,而是发送将处理结果值的例程:
function(query, callback) {
myApi.exec('SomeCommand', function(response) {
// other stuff here...
// bla bla..
callback(response); // this will "return" your value to the original caller
});
}
Run Code Online (Sandbox Code Playgroud)
所以你不要这样使用它:
var returnValue = myFunction(query);
Run Code Online (Sandbox Code Playgroud)
但是像这样:
myFunction(query, function(returnValue) {
// use the return value here instead of like a regular (non-evented) return value
});
Run Code Online (Sandbox Code Playgroud)
Tim*_*imo 26
实现此目的的一种方法是将API调用包装到promise中,然后使用await
等待结果.
// let's say this is the API function with two callbacks,
// one for success and the other for error
function apiFunction(query, successCallback, errorCallback) {
if (query == "bad query") {
errorCallback("problem with the query");
}
successCallback("Your query was <" + query + ">");
}
// myFunction wraps the above API call into a Promise
// and handles the callbacks with resolve and reject
function apiFunctionWrapper(query) {
return new Promise((resolve, reject) => {
apiFunction(query,(successResponse) => {
resolve(successResponse);
}, (errorResponse) => {
reject(errorResponse)
});
});
}
// now you can use await to get the result from the wrapped api function
// and you can use standard try-catch to handle the errors
async function businessLogic() {
try {
const result = await apiFunctionWrapper("query all users");
console.log(result);
// the next line will fail
const result2 = await apiFunctionWrapper("bad query");
} catch(error) {
console.error("ERROR:" + error);
}
}
// call the main function
businessLogic();
Run Code Online (Sandbox Code Playgroud)
输出:
Your query was <query all users>
ERROR:problem with the query
Run Code Online (Sandbox Code Playgroud)
Luc*_*ato 23
检查一下:https: //github.com/luciotato/waitfor-ES6
你的代码与wait.for :(需要生成器, - 和谐标志)
function* (query) {
var r = yield wait.for( myApi.exec, 'SomeCommand');
return r;
}
Run Code Online (Sandbox Code Playgroud)
vis*_*tel 10
如果您不想使用回叫,则可以使用"Q"模块.
例如:
function getdb() {
var deferred = Q.defer();
MongoClient.connect(databaseUrl, function(err, db) {
if (err) {
console.log("Problem connecting database");
deferred.reject(new Error(err));
} else {
var collection = db.collection("url");
deferred.resolve(collection);
}
});
return deferred.promise;
}
getdb().then(function(collection) {
// This function will be called afte getdb() will be executed.
}).fail(function(err){
// If Error accrued.
});
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅:https://github.com/kriskowal/q
如果你想要它非常简单和容易,没有花哨的库,等待在节点中执行回调函数,在执行其他代码之前,是这样的:
//initialize a global var to control the callback state
var callbackCount = 0;
//call the function that has a callback
someObj.executeCallback(function () {
callbackCount++;
runOtherCode();
});
someObj2.executeCallback(function () {
callbackCount++;
runOtherCode();
});
//call function that has to wait
continueExec();
function continueExec() {
//here is the trick, wait until var callbackCount is set number of callback functions
if (callbackCount < 2) {
setTimeout(continueExec, 1000);
return;
}
//Finally, do what you need
doSomeThing();
}
Run Code Online (Sandbox Code Playgroud)
现在是 2020 年,API 很可能已经有了一个可与 wait 配合使用的基于 Promise 的版本。但是,某些接口,尤其是事件发射器将需要此解决方法:
// doesn't wait
let value;
someEventEmitter.once((e) => { value = e.value; });
Run Code Online (Sandbox Code Playgroud)
// waits
let value = await new Promise((resolve) => {
someEventEmitter.once('event', (e) => { resolve(e.value); });
});
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下,它将是:
let response = await new Promise((resolve) => {
myAPI.exec('SomeCommand', (response) => { resolve(response); });
});
Run Code Online (Sandbox Code Playgroud)
过去 3 年(自 v7.6 起),Await 一直存在于新的 Node.js 版本中。
小智 6
从节点 4.8.0 开始,您可以使用称为生成器的 ES6 功能。您可以按照本文了解更深入的概念。但基本上你可以使用生成器和承诺来完成这项工作。我正在使用bluebird来承诺和管理生成器。
您的代码应该没问题,就像下面的例子。
const Promise = require('bluebird');
function* getResponse(query) {
const r = yield new Promise(resolve => myApi.exec('SomeCommand', resolve);
return r;
}
Promise.coroutine(getResponse)()
.then(response => console.log(response));
Run Code Online (Sandbox Code Playgroud)
注意:此答案可能不应在生产代码中使用.这是一个黑客,你应该知道其含义.
有uvrun模块(在这里更新了更新的Nodejs版本),你可以在其中执行libuv主事件循环(这是Nodejs主循环)的单循环循环.
您的代码如下所示:
function(query) {
var r;
myApi.exec('SomeCommand', function(response) {
r = response;
});
var uvrun = require("uvrun");
while (!r)
uvrun.runOnce();
return r;
}
Run Code Online (Sandbox Code Playgroud)
(您可以替代使用uvrun.runNoWait()
.这可以避免阻塞的一些问题,但需要100%的CPU.)
请注意,这种方法会使Nodej的整个目的无效,即使所有内容都异步和非阻塞.此外,它可能会大大增加您的callstack深度,因此最终可能会出现堆栈溢出.如果你递归地运行这样的函数,你肯定会遇到麻烦.
请参阅有关如何重新设计代码以"正确"执行此操作的其他答案.
这里的解决方案可能仅在您进行测试和esp时才有用.想要同步和串行代码.