[英]Running code sequentially in Node.js
I have a function that fetches data from a database: 我有一个从数据库中获取数据的函数:
recentItems = function () {
Items.find({item_published: true}).exec(function(err,item){
if(!err)
return item
});
};
And I want to use it like this: 我想像这样使用它:
var x = recentItems();
But this fails with undefined value due to Async behavior of recentItems
. 但是由于
recentItems
异步行为,这会因未定义的值而失败。 I know that I can change my function to use a callback like this: 我知道我可以改变我的函数来使用这样的回调:
recentItems = function (callback) {
Items.find({item_published: true}).exec(function(err,item){
if(!err)
callback(item)
});
};
And: 和:
recentItems(function(result){
var x = result;
});
But i dont want to use this method because i have a situation like this. 但我不想使用这种方法,因为我有这样的情况。 i have a function that should do two operations and pus result to an array and after them, fire a callback and return value:
我有一个函数应该执行两个操作,并将pus结果发送到一个数组,在它们之后,触发一个回调并返回值:
var calc = function(callback){
var arr = [];
var b = getValues();
arr.push(b);
recentItems(function(result){
var x = result;
arr.push(x);
});
callback(arr);
};
In this situation, the value of b
pushed to arr
and the main callback called and after that value of x
fetched from recentItems
duo to Async behavior of recentItems
. 在这种情况下,
b
的值被推送到arr
和主要的回调调用,并且在x
值从recentItems
duo获取到recentItems
Async行为recentItems
。 But I need this two operation runs sequentially and one after one. 但我需要这两个操作顺序并一个接一个地运行。 After calculating all of them, then last line runs and the callback fired.
在计算了所有这些之后,最后一行运行并且回调被触发。
How can I resolve this? 我该如何解决这个问题? I read about the
Promises
and Async
libraries, but I don't know which of them is my answer. 我读到了
Promises
和Async
库,但我不知道它们中的哪一个是我的答案。 Can I overcome this with raw Node.js? 我可以用原始Node.js克服这个问题吗? If so, I would prefer that.
如果是这样,我宁愿这样做。
There are some ways of doing what you want, but none of them are ~perfect~ yet. 有一些方法可以做你想要的,但是它们都没有〜完美〜。
There is an ES7 proposal of native async/await
that will be the callback heaven , but atm, you can do: 有一个原生
async/await
的ES7提议将成为回调天堂 ,但atm,你可以这样做:
But, if you're already using the newest version of NodeJS (4.0.0 as the time of writing) - and if you're not, you really should - the best way of achieving what you want is to use generators . 但是,如果您已经在使用最新版本的NodeJS(4.0.0作为编写时) - 如果您不是,那么您真的应该 - 实现您想要的最佳方式是使用生成器 。
Combined with a small library named co , it will help you to achieve almost what the ES7 async/await
proposes, and it will mostly use native code, so both readability and performance are really good: 结合一个名为co的小型库,它将帮助您实现ES7
async/await
提出的几乎所有,并且它将主要使用本机代码,因此可读性和性能都非常好:
var co = require('co');
var calc = co(function *calc() {
var arr = [];
var b = getValues();
arr.push(b);
var items = yield recentItems();
arr.push(items);
return arr;
});
function recentItems() {
return new Promise(function(resolve) {
Items.find({item_published: true}).exec(function(err, item) {
if(!err)
resolve(item);
});
}
You can read more about this subject in this awesome Thomas Hunter's blog post . 你可以在这篇很棒的Thomas Hunter博客文章中阅读更多关于这个主题的内容 。
You've almost got it. 你几乎得到了它。 There is no method to work-around callbacks.
没有办法解决回调问题。 However, you can certainly use callbacks to do what you want.
但是,您当然可以使用回调来执行您想要的操作。 Simply nest them:
简单地嵌套它们:
var calc = function(callback){
var arr = [];
getValues(function(b){
arr.push(b);
recentItems(function(result){
var x = result;
arr.push(x);
callback(arr);
});
});
};
You can try something like this. 你可以尝试这样的事情。 It still nests the callbacks, but the code is a little cleaner.
它仍然存在回调,但代码更清晰一些。
var callA = function(callback) {
//Run the first call
prompt(callback(data));
}
var callB = function(callback) {
//Some other call
prompt(callback(data));
}
callA(function(dataA) {
callB(function(dataB) {
//Place a handler function here
console.log(dataA + " " + dataB)
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.