Javascript 承诺已解决后如何从承诺对象中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27550394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fetch value from Promise object after promise has been resolved
提问by Vikram
Please note This is a contrived example.
请注意这是一个人为的例子。
function longFunc(){
var deferred = $.Deferred();
setTimeout(function(){
console.log("long func completed");
deferred.resolve("hello");
}, 3000);
return deferred.promise();
}
function shortAfterLongFunc(x){
console.log('short func completed with value: ' + x);
return {
a: x
};
}
processFurther(longFunc().then(shortAfterLongFunc)); // send the array for further processing
Problem
问题
I am unable to figure out how to return any kind of object/functionfor further downstream processing after shortAfterLongFunccompletes. I can console.log from shortAfterLongFuncbut that's not what i require here.
Fiddle Here
我无法弄清楚如何在shortAfterLongFunc完成后返回任何类型的对象/函数以进行进一步的下游处理。我可以从 console.log ,shortAfterLongFunc但这不是我在这里需要的。
在这里小提琴
Thanks for looking!
感谢您的关注!
UPDATE:
更新:
Okay just to make my question slightly better...this is a simple use case I am looking at:
好的,只是为了让我的问题稍微好一点……这是我正在查看的一个简单用例:
$.map(['H','E','L','L', 'O'], somefunc). // for each item in array apply somefunc function
function somefunc(x){ // gets called for each value 'H', 'E' etc. in the array by $.map()
var longfunc = function(y){
var deferred = $.Deferred();
setTimeout(function(){
console.log("long func completed");
deferred.resolve(y.toLocaleLowerCase());
}, 3000);
return deferred.promise();
};
var shortAfterLongFunc = function(x){
console.log('short func completed with value: ' + x);
return x;
}
// What should I do here
return longFunc(x).then(shortAfterLongFunc); // must return lower case char to the caller of someFunc
}
somefunc()lets say processes each element of Array to lower case. However, assume this processing takes a long time and async (think setTimeout).. hence a promise to ensure synchronous operation for each element...but on using promise I find myself not able return the transformed value
somefunc()假设将 Array 的每个元素处理为小写。然而,假设这个处理需要很长时间并且是异步的(想想 setTimeout)。因此承诺确保每个元素的同步操作......但是在使用承诺时我发现自己无法返回转换后的值
采纳答案by dfsq
Just chain another thencall, since shortAfterLongFuncreturns new promise you can further work with it:
只需链接另一个then调用,因为shortAfterLongFunc返回新的承诺,您可以进一步使用它:
longFunc().then(shortAfterLongFunc).then(function(data) {
console.log('all is complted', data);
});
Demo: http://jsfiddle.net/ebt4pxxa/2/
演示:http: //jsfiddle.net/ebt4pxxa/2/
回答by Mohsen
There is a trick, define an array or object and value it in then:
有一个技巧,定义一个数组或对象并在其中赋值:
let Result=[];
let callImport = (load)=>{
import('./component.js').
then((Module)=>{
load[0] = Module;
});};
callImport(Result);
setTimeout(()=> console.log(Result[0]),10);
Here i used setTimeout as await to prevent print Result before promise execution finish
在这里,我使用 setTimeout 作为 await 以防止在 promise 执行完成之前打印结果

