我使用这个代码与快速路由和纳米:
router.get('/', function (request, response) {
db.view('designdoc', 'bydate', { 'descending': true })
.then(results => {
// data manipulation of results, all blocking and fine
return results;
})
.then(results => {
nano.uuids(1)
.then(uuids => {
results.uuid = uiids['uuids'][0];
resolve(); // return ?
})
.catch(error => {
// ?
});
});
return results;
})
.then(results => { response.json(results); }) // how to have results.uuid = something from the previous then ?
.catch(error => { response.json(error); });
我想从nano.uuid
添加一个uuid到结果,但我无法弄清楚如何操纵下一个then
内的承诺。
如何从nano.uuid
获取数据,等待它并将其添加到results
?
我正在转向@ narayansharma91建议的异步方法,这段代码解决了我的问题:
router.get('/', async function (request, response) {
const results = await db.view('designdoc', 'bydate', { 'descending': true });
var uuid = await nano.uuids(1);
results.uuid = uuid.uuids[0];
response.json(results);
}
但我仍然希望了解基于承诺的解决方案。
您不需要执行此长脚本来获取结果。您可以使用async/await
执行相同的操作,如下所示。
router.get('/', async function (request, response) {
const result = await db.view('designdoc', 'bydate', { 'descending': true })
console.log(result)//do what ever you want to with result
});
function successcb(results){
return new Promise(function(resolve, reject) {
nano.uuids(1)
.then(uuids => {
results.uuid = uiids['uuids'][0];
resolve(results);
})
.catch(error => {
throw err;
});
});
}
router.get('/', function (request, response) {
db.view('designdoc', 'bydate', { 'descending': true })
.then(results => {
return results;
})
.then(successcb)
.then(results => { response.json(results); })
.catch(error => { response.json(error); });
我没有改变你的代码,因为你想要一个承诺的解决方案。您不需要调用resolve(),因为.then()会为您执行此操作。将uuids附加到结果后,只需返回结果。
router.get('/', function (request, response) {
db.view('designdoc', 'bydate', {
'descending': true
})
.then(results => {
// data manipulation of results, all blocking and fine
return results;
})
.then(results => {
results = nano.uuids(1)
.then(uuids => {
results.uuid = uiids['uuids'][0];
return results;
})
.catch(error => {
throw error;
});
return results;
}).then(results => {
response.json(results);
}) // how to have results.uuid = something from the previous then ?
.catch(error => {
response.json(error);
});
})
router.get('/', function(request, response) {
let _results;
db.view('designdoc', 'bydate', {
'descending': true
})
.then(results => {
// data manipulation of results, all blocking and fine
// save results in tempopary variable
// since we want to use only one level of Promise chains
// we need to save results on controller scope level to allow access in further chains
_results = results;
// we don't need to return since every `then` by default return `Promise`
})
.then(() => {
// you can just return, since nano.uuids return Promise
// next then chain will be called once action completed
return nano.uuids();
})
.then(uuids => {
// use _results from controller level scope
// uuids will be injected as a result of return nano.uuids()
_results.uuid = uiids['uuids'][0];
response.json(_results);
})
.catch(error => {
response.json(error);
});
}
为了更好地理解Promise
链,我总是建议阅读这个great article