[英]How to write my function with Promises
I am load HTML (external app) into an iFrame
我正在将HTML(外部应用程序)加载到iFrame
I want to "do" something ( callback
) when an element becomes available in my iFrame
. 当一个元素在我的iFrame
变得可用时,我想“做”某事( callback
)。 Here how I wrote it, and I'd like to write this with Promises instead: 这里是我的写法,我想用Promises来写:
function doWhenAvailable(selector, callback) {
console.warn("doWhenAvailable", selector)
if ($('#myiFrame').contents().find(selector).length) {
var elt = $('#myiFrame').contents().find(selector);
console.info("doWhenAvailable Found", elt)
callback && callback(elt);
} else {
setTimeout(function() {
doWhenAvailable(selector, callback);
}, 1000);
}
}
Actually instead of using setTimeout
, I'd like to use setInterval
to repeat the "find element" until it's found and resolve the "promise". 实际上,我不想使用setTimeout
,而是要使用setInterval
重复“查找元素”直到找到并解决“承诺”。
No, you would not use setInterval
, you just would wrap the timeout in a promise and drop the callback: 不,您不会使用setInterval
,只是将超时包装在promise中并删除回调:
function wait(t) {
return new Promise(function(resolve) {
setTimeout(resolve, t);
});
}
function whenAvailable(selector) {
var elt = $('#myiFrame').contents().find(selector);
if (elt.length)
return Promise.resolve(elt);
else
return wait(1000).then(function() {
return whenAvailable(selector);
});
}
Keeping your recursive style, it would have become something like that : 保持您的递归风格,它会变成这样:
function doWhenAvailable(selector) {
var dfd = jQuery.Deferred();
console.warn("doWhenAvailable", selector)
if ($('#myiFrame').contents().find(selector).length) {
var elt = $('#myiFrame').contents().find(selector);
console.info("doWhenAvailable Found", elt)
return dfd.resolve(elt);
} else {
setTimeout(function() {
doWhenAvailable(selector).then(function(e) {
dfd.resolve(e);
});
}, config[env].wrapper.timeOutInMs);
}
return dfd.promise();
}
But I would have tried to avoid recursive calls here 但是我会尽量避免在这里递归调用
The general idea is to return a promise instead of receiving a callback. 一般的想法是返回一个promise,而不是接收回调。
Example: 例:
var xpto = function(res) {
return new Promise((resolve, reject) => {
if(res > 0) resolve('Is greater');
else reject(new Error('is lower'));
});
}
So in your case: 因此,在您的情况下:
function doWhenAvailable(selector) {
function work(callback) {
if ($('#myiFrame').contents().find(selector).length) {
var elt = $('#myiFrame').contents().find(selector);
console.info("doWhenAvailable Found", elt)
callback(elt);
}
}
return new Promise((resolve, reject) => {
console.warn("doWhenAvailable", selector)
setInterval(() => work(resolve), 1000);
})
}
Here: 这里:
function doWhenAvailable(selector) {
return new Promise(function(resolve, reject){
console.warn("doWhenAvailable", selector)
if ($('#myiFrame').contents().find(selector).length) {
var elt = $('#myiFrame').contents().find(selector);
console.info("doWhenAvailable Found", elt)
resolve(elt);
} else {
setTimeout(function() {
doWhenAvailable(selector).then(function(data){
resolve(data);
});
}, config[env].wrapper.timeOutInMs);
}
}
}
And call your function like that: 然后像这样调用您的函数:
doWhenAvailable("#elemId").then(function(elt){
//do what you want
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.