我想运行“setval”之后完成的功能是“afterfirstfunc”。函数将执行 [英] I want to run "setval" function completed after then that "afterfirstfunc" function will go to execute

查看:88
本文介绍了我想运行“setval”之后完成的功能是“afterfirstfunc”。函数将执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行setVal函数之后完成afterFirstFunc函数将执行,

var setVal = function(){
var DFD = $递延();
for(var i = 0; i< 5; i ++){
(function(i){
setTimeout(function(){
console.log(Inside First) + i);
},1000 * i);
})(i);
};
dfd.resolve();
return dfd.promise();
};
var setVal1 = function(){
var dfd1 = $ .Deferred();
for(var i = 0; i< 5; i ++){
(function(i){
setTimeout(function(){
console.log(Inside Second + i);
},1000 * i);
})(i);
};
dfd1.resolve();
return dfd1.promise();
};
setVal()。then(function(){
setVal1();
});





我尝试了什么:



i想先执行setVal1然后setVal1将执行,不是两个函数并行工作,我已经尝试了所有的东西但没有任何帮助我实现这个

解决方案

.Deferred();
for(var i = 0; i< 5; i ++){
(function(i){
setTimeout(function(){
console.log(Inside First) + i);
},1000 * i);
})(i);
};
dfd.resolve();
return dfd.promise();
};
var setVal1 = function(){
var dfd1 =


.Deferred();
for(var i = 0; i< 5; i ++){
(function(i){
setTimeout(function(){
console.log(Inside Second + i);
},1000 * i);
})(i);
};
dfd1.resolve();
return dfd1.promise();
};
setVal()。then(function(){
setVal1();
});





我尝试了什么:



i想先执行setVal1然后setVal1将执行,并不是两个函数并行工作,我已经尝试了所有的东西,但没有任何帮助我实现这个


你解决从 setVal 之前任何 setTimeout 调用已执行。



你需要等到所有 setTimeout 调用完成后再解析promise。您需要为每个调用创建单独的承诺,并使用承诺.all [ ^ ]等待他们全部完成。

  var  delay = < span class =code-keyword> function (时间,价值){
return new 承诺 function (解析){
setTimeout(resolve.bind(< span class =code-keyword> null ,value),time);
});
};

承诺 .prototype.delay = 功能(时间){
返回 .then( function (价值){
返回延迟(时间,价值);
});
};

var setVal = function (){
var promises = [];
for var i = 0 ; i< 5 ; i ++){
promises.push( Promise .resolve (i).delay(i * 1000 )。then( function (i){
console .log( 首先在内部 ,i);
}));
}

return 承诺 .all(promises);
};

var setVal1 = function (){
var promises = [];
for var i = 0 ; i< 5 ; i ++){
promises.push( Promise .resolve (i).delay(i * 1000 )。then( function (i){
console .log( Inside second ,i);
}));
}

return 承诺 .all(promises);
};

setVal()。then(setVal1);



javascript - 在promise链上使用setTimeout - Stack Overflow [ ^ ]

承诺 - JavaScript | MDN [ ^ ]


i want to run "setVal" function Completed after then that "afterFirstFunc" function will go to execute,

var setVal = function() {
var dfd=$.Deferred();
for (var i = 0; i < 5; i++) {
(function (i) {
setTimeout(function () {
console.log("Inside First "+i);
}, 1000*i);
})(i);
};
dfd.resolve();
return dfd.promise();
};
var setVal1=function(){
var dfd1=$.Deferred();
for (var i = 0; i < 5; i++) {
(function (i) {
setTimeout(function () {
console.log("Inside Second "+i);
}, 1000*i);
})(i);
};
dfd1.resolve();
return dfd1.promise();
};
setVal().then(function(){
setVal1(); 
});



What I have tried:

i want to execute setVal1 run first then setVal1 will go to execute, not both the function work parallel, i have tried all the thing but nothing help me to achieve this

解决方案

.Deferred(); for (var i = 0; i < 5; i++) { (function (i) { setTimeout(function () { console.log("Inside First "+i); }, 1000*i); })(i); }; dfd.resolve(); return dfd.promise(); }; var setVal1=function(){ var dfd1=


.Deferred(); for (var i = 0; i < 5; i++) { (function (i) { setTimeout(function () { console.log("Inside Second "+i); }, 1000*i); })(i); }; dfd1.resolve(); return dfd1.promise(); }; setVal().then(function(){ setVal1(); });



What I have tried:

i want to execute setVal1 run first then setVal1 will go to execute, not both the function work parallel, i have tried all the thing but nothing help me to achieve this


You resolve the promise returned from setVal before any of the setTimeout calls have executed.

You need to wait until all of the setTimeout calls have completed before resolving the promise. You'll want to create a separate promise for each call, and use Promise.all[^] to wait for them all to complete.

var delay = function(time, value){
    return new Promise(function(resolve) {
        setTimeout(resolve.bind(null, value), time);
    });
};

Promise.prototype.delay = function(time) {
    return this.then(function(value) {
        return delay(time, value);
    });
};

var setVal = function(){
    var promises = [];
    for (var i = 0; i < 5; i++){
        promises.push(Promise.resolve(i).delay(i * 1000).then(function(i){
            console.log("Inside first", i);
        }));
    }
    
    return Promise.all(promises);
};

var setVal1 = function(){
    var promises = [];
    for (var i = 0; i < 5; i++){
        promises.push(Promise.resolve(i).delay(i * 1000).then(function(i){
            console.log("Inside second", i);
        }));
    }
    
    return Promise.all(promises);
};

setVal().then(setVal1);


javascript - using setTimeout on promise chain - Stack Overflow[^]
Promise - JavaScript | MDN[^]


这篇关于我想运行“setval”之后完成的功能是“afterfirstfunc”。函数将执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆