Javascript 如何使用.map()将承诺的结果分配给数组元素?
我在这里使用AngularJS,但是如果你有一个更一般的答案,我很高兴知道 我有一个包含ID的元素数组Javascript 如何使用.map()将承诺的结果分配给数组元素?,javascript,angularjs,arrays,promise,array.prototype.map,Javascript,Angularjs,Arrays,Promise,Array.prototype.map,我在这里使用AngularJS,但是如果你有一个更一般的答案,我很高兴知道 我有一个包含ID的元素数组 myArray = [{ id: 0, foo: "foo" }, { id: 1, bar: "bar" }] 以及一个使用这些ID请求信息的服务,这些ID将一个信息对象(用于请求)和两个回调作为参数,一个用于成功,一个用于错误(这两个都不是必需的) 我想做的是这样的: myArray.map(function (element){ Service.getInforma
myArray = [{
id: 0,
foo: "foo"
}, {
id: 1,
bar: "bar"
}]
以及一个使用这些ID请求信息的服务,这些ID将一个信息对象(用于请求)和两个回调作为参数,一个用于成功,一个用于错误(这两个都不是必需的)
我想做的是这样的:
myArray.map(function (element){
Service.getInformations({id: element.id}, function(data) {
return data; // here is the issue
})
});
这里的问题是,我在服务回调函数中返回数据,而不是在映射回调函数中返回数据。我正在努力寻找一些好方法来实现它。链接异步函数是一个棘手的老问题。下面是一种在香草Javascript中实现的方法(带或不带角度):
Promise.all(myArray.map(item => new Promise((resolve, reject) => {
Service.getInformations({id: item.id}, resolve, reject)
}).then((resultArray) => {
//reduce result
}).catch((errorArray)=> {
//reduce error
})
var myArray=[
{
id:0,
福:“福”
},
{
id:1,
酒吧:“酒吧”
}
];
var asyncChain=函数(myFunction){
var nextThingToDo=函数(){
myFunction(下一个ThingTodo);
}
nextThingToDo();
}
//在本例中,myFunction是getArrayItemInfo
//下一个thingtodo是goToNextArray项
//Express.js和Node就是这样做的
//是的,下一个想法是Todo在自我召唤
//如果这看起来很奇怪,那是因为它是。耶,Javascript。
var指数=0;
var getArrayItemInfo=函数(goToNextArrayItem){
if(索引
下面是一个简单的示例,您可以尝试不使用角度:
var myArray = [
{
id: 0,
foo: "foo"
},
{
id: 1,
bar: "bar"
}
];
var asyncChain = function(myFunction){
var nextThingToDo = function(){
myFunction(nextThingToDo);
}
nextThingToDo();
}
// In this example, myFunction is getArrayItemInfo
// nextThingToDo is goToNextArray Item
// This is how Express.js and Node do things
// Yes, nextThingToDo is calling itself
// If this seems weird, it's because it is. Yay, Javascript.
var index = 0;
var getArrayItemInfo = function(goToNextArrayItem){
if(index < myArray.length){
Service.getInformations({id: index}, function(data){
myArray[index].data = data; // You can do something with the data here
index += 1;
goToNextArrayItem(); // Move on to the next item when the asynchronous part is done
// Without this, execution would stop here
});
}
}
asyncChain(getArrayItemInfo);
var myArray=[“一秒”、“两秒”、“三秒”、“仅此而已];
var asyncChain=函数(myFunction){
var next=函数(){
myFunction(下一步);
}
next();
}
var指数=0;
var getArrayItemInfo=函数(goToNextArrayItem){
if(索引 ”);
指数+=1;
goToNextArrayItem();
}, 1000);
}
}
异步链(getArrayItemInfo)代码>您可以这样做,在angular上使用内置的$q服务
您可以迭代元素,调用服务并返回承诺,只有在所有异步操作完成时才执行回调
在本例中,回调从它们各自的调用中获取一个结果数组
var-app=angular.module(“sampleApp”,[]);
app.controller(“sampleController”、[“$scope”、“$q”、“sampleService”,
功能($scope,$q,sampleService){
var randomArray=[1,2,3,4,5,6];
var promisesArray=randomArray.map((输入)=>{
返回sampleService.getInformations(输入)
});
$q.all(promisesArray)。然后(函数(outputArray){
log(“结果数组”);
控制台日志(输出阵列);
});
}
]);
app.service(“sampleService”,function()){
this.getInformations=函数(值){
var承诺=新承诺(功能(解决、拒绝){
setTimeout(函数(){
值=值*2;
决心(价值);
}, 1000);
});
回报承诺;
};
});代码>
如果没有解释,这个答案是没有用的。好的,我不知道,谢谢。我的答案似乎更准确。然而,下面的答案要有趣得多(RobertAKARobin&Sreekanth)
var myArray = [
{
id: 0,
foo: "foo"
},
{
id: 1,
bar: "bar"
}
];
var asyncChain = function(myFunction){
var nextThingToDo = function(){
myFunction(nextThingToDo);
}
nextThingToDo();
}
// In this example, myFunction is getArrayItemInfo
// nextThingToDo is goToNextArray Item
// This is how Express.js and Node do things
// Yes, nextThingToDo is calling itself
// If this seems weird, it's because it is. Yay, Javascript.
var index = 0;
var getArrayItemInfo = function(goToNextArrayItem){
if(index < myArray.length){
Service.getInformations({id: index}, function(data){
myArray[index].data = data; // You can do something with the data here
index += 1;
goToNextArrayItem(); // Move on to the next item when the asynchronous part is done
// Without this, execution would stop here
});
}
}
asyncChain(getArrayItemInfo);