Javascript 我如何从这个承诺中返回数组?
我已经尝试了一些方法并进行了全面的阅读,但是我似乎不知道如何从这个函数返回names数组Javascript 我如何从这个承诺中返回数组?,javascript,bluebird,es6-promise,Javascript,Bluebird,Es6 Promise,我已经尝试了一些方法并进行了全面的阅读,但是我似乎不知道如何从这个函数返回names数组 function getNames(oauth2Client, docs) { const api = x('v1'); let names = []; return Promise.each(docs, function(doc) { let req = api.users.messages.get; let options = ({ auth
function getNames(oauth2Client, docs) {
const api = x('v1');
let names = [];
return Promise.each(docs, function(doc) {
let req = api.users.messages.get;
let options = ({
auth: oauth2Client,
'userId': 'me',
'id': doc.id
});
return Promise.promisify(req)(options).then(function(response) {
for (y = 0; y < response.names.length; y++) {
names.push(response.names[y].toLowerCase());
}
})
.catch(function (err) {
console.log('An error occured: ' + err.message);
throw err;
});
});
}
函数getNames(oauth2Client,文档){
常数api=x('v1');
让名称=[];
退货承诺。每个(单据、功能(单据){
让req=api.users.messages.get;
让选项=({
作者:oauth2Client,
'userId':'me',
“id”:doc.id
});
返回承诺。承诺(req)(选项)。然后(功能(响应){
对于(y=0;y
我不确定您使用的Promise库是什么,因为它看起来是非标准的,但我认为这样的东西正是您想要的。我为正在发生的事情添加了注释-您可能必须更改这些代码行以适合您的promise库
function getNames(oauth2Client, docs) {
const api = x('v1');
const names = [];
// create a stack of promises
const stack = [];
docs.forEach(doc => {
let req = api.users.messages.get;
let options = ({
auth: oauth2Client,
'userId': 'me',
'id': doc.id
});
// push each promise onto the stack
stack.push(
Promise.promisify(req)(options).then(function(response) {
for (y = 0; y < response.names.length; y++) {
names.push(response.names[y].toLowerCase());
}
})
.catch(function (err) {
console.log('An error occured: ' + err.message);
throw err;
})
);
});
// Wait for all promises in the stack to finish, and then
// return the names array as the final value.
return Promise.all(stack).then(() => names);
}
函数getNames(oauth2Client,文档){
常数api=x('v1');
常量名称=[];
//创造一堆承诺
常量堆栈=[];
docs.forEach(doc=>{
让req=api.users.messages.get;
让选项=({
作者:oauth2Client,
'userId':'me',
“id”:doc.id
});
//将每个承诺推到堆栈上
堆栈推送(
承诺。承诺(要求)(选项)。然后(功能(响应){
对于(y=0;y名称);
}
我不确定您使用的Promise库是什么,因为它看起来是非标准的,但我认为这样的东西正是您想要的。我为正在发生的事情添加了注释-您可能必须更改这些代码行以适合您的promise库
function getNames(oauth2Client, docs) {
const api = x('v1');
const names = [];
// create a stack of promises
const stack = [];
docs.forEach(doc => {
let req = api.users.messages.get;
let options = ({
auth: oauth2Client,
'userId': 'me',
'id': doc.id
});
// push each promise onto the stack
stack.push(
Promise.promisify(req)(options).then(function(response) {
for (y = 0; y < response.names.length; y++) {
names.push(response.names[y].toLowerCase());
}
})
.catch(function (err) {
console.log('An error occured: ' + err.message);
throw err;
})
);
});
// Wait for all promises in the stack to finish, and then
// return the names array as the final value.
return Promise.all(stack).then(() => names);
}
函数getNames(oauth2Client,文档){
常数api=x('v1');
常量名称=[];
//创造一堆承诺
常量堆栈=[];
docs.forEach(doc=>{
让req=api.users.messages.get;
让选项=({
作者:oauth2Client,
'userId':'me',
“id”:doc.id
});
//将每个承诺推到堆栈上
堆栈推送(
承诺。承诺(要求)(选项)。然后(功能(响应){
对于(y=0;y名称);
}
只需添加
return Promise.each(…)
.then(function() {
return names;
});
这将导致返回的承诺使用名称
数组来实现
但是,我建议您不要在每个
循环中使用全局数组,特别是如果您关心结果的顺序的话。相反,用一个值解析每个承诺,使用map
而不是each
,并在最后合并结果:
const api = x('v1');
const getUserMessages = Promise.promisify(api.users.messages.get);
function getNames(oauth2Client, docs) {
return Promise.map(docs, doc =>
getUserMessages({
auth: oauth2Client,
'userId': 'me',
'id': doc.id
})
.then(response =>
response.names.map(name => name.toLowerCase());
)
)
.then(nameArrays =>
[].concat(...nameArrays)
);
}
简单地加上
return Promise.each(…)
.then(function() {
return names;
});
这将导致返回的承诺使用名称
数组来实现
但是,我建议您不要在每个
循环中使用全局数组,特别是如果您关心结果的顺序的话。相反,用一个值解析每个承诺,使用map
而不是each
,并在最后合并结果:
const api = x('v1');
const getUserMessages = Promise.promisify(api.users.messages.get);
function getNames(oauth2Client, docs) {
return Promise.map(docs, doc =>
getUserMessages({
auth: oauth2Client,
'userId': 'me',
'id': doc.id
})
.then(response =>
response.names.map(name => name.toLowerCase());
)
)
.then(nameArrays =>
[].concat(...nameArrays)
);
}
避开这个!谢谢顺便说一句,承诺图书馆是蓝鸟,非常感谢,这是完美的第一次,并感谢额外的评论。避免!谢谢顺便说一句,承诺图书馆是蓝鸟,非常感谢,这是完美的第一次,并感谢额外的评论。