如何从承诺中返回价值 [英] How to return value from a Promise
问题描述
我一直在与Promises挣扎,想知道事情在Promises中如何工作。
在我的项目中,我正在使用Bookshelfjs ORM从Postgres中获取数据。
I have been struggling with Promises and would like to know how things work with Promises. In my project, I am using Bookshelfjs ORM for fetching the data from Postgres.
这是我现在正在处理的代码。我在此请求中获得了一组设备ID,每个设备都以两种模式之一运行。
Here is the code that I am working on right now. I get an array of device ids in this request and each device is running in one of the two modes.
router.post('/devices', function (req, res, next) {
var currentData = [];
var deviceIds = req.body.devices;
loadash.forEach(deviceIds, function (device) {
var deviceid = device.deviceid;
Device.forge()
.where({deviceid: deviceid})
.fetch({columns: ['id', 'mode']})
.then(function (fetchedDevice) {
if(fetchedDevice.get('mode') === 1) {
Model_1.forge()
.where({device_id: fetchedDevice.get('id')})
.orderBy('epoch_time', 'DESC')
.fetch()
.then(function (modelOne) {
//first push
currentData.push(modelOne.toJSON());
//array with first push data
console.log(currentData)
})
.catch(function (err) {
console.log(err);
});
}
else if(fetchedDevice.get('mode') === 2) {
Model_2.forge()
.where({device_id: fetchedDevice.get('id')})
.orderBy('epoch_time', 'DESC')
.fetch()
.then(function (modelTwo) {
//second push
currentData.push(modelTwo.toJSON());
//array not empty here(shows data from both push)
console.log(currentData);
})
.catch(function (err) {
console.log(err);
});
}
})
.catch(function (err) {
console.log(err);
});
});
//This shows an empty array
console.log('Final: ' +currentData);
});
现在,我知道这是由于Javascript的异步特性而发生的。我的问题是
Now, I know this is happening because of async nature of Javascript. My question is
-
在所有
push()$ c $之后,如何显示最终数组? c>已经执行了吗?我尝试使用
Promise.all()
方法执行此操作,但没有成功。
how can I display final array after all
push()
have been executed? I tried doing this usingPromise.all()
method but did not have any success.
是是否有可能在每个promise中返回 modelOne
或 modelTwo
然后推送到数组?我该如何实现?
is it possible to return modelOne
or modelTwo
out of every promise and then push to an array? How can I achieve this?
推荐答案
使用 .map()
和 Promise.all()
, return
从函数传递给 .then()
Use .map()
and Promise.all()
, return
a value from function passed to .then()
var currentData = loadash.map(deviceIds, function (device) {
var deviceid = device.deviceid;
return Device.forge()
.where({deviceid: deviceid})
.fetch({columns: ['id', 'mode']})
.then(function (fetchedDevice) {
if(fetchedDevice.get('mode') === 1) {
// return value from `.then()`
return Model_1.forge()
.where({device_id: fetchedDevice.get('id')})
.orderBy('epoch_time', 'DESC')
.fetch()
.then(function (modelOne) {
// return value from `.then()`
return modelOne.toJSON();
})
.catch(function (err) {
console.log(err);
});
}
else if(fetchedDevice.get('mode') === 2) {
// return value from `.then()`
return Model_2.forge()
.where({device_id: fetchedDevice.get('id')})
.orderBy('epoch_time', 'DESC')
.fetch()
.then(function (modelTwo) {
// return value from `.then()`
return modelTwo.toJSON();
})
}
})
});
var res = Promise.all(currentData);
res
.then(function(results) {console.log(results)})
.catch(function (err) {
console.log(err);
});
这篇关于如何从承诺中返回价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!