lag*_*lex 77 javascript settimeout promise
这不是一个现实世界的问题,我只是想了解如何创造承诺.
我需要了解如何为一个不返回任何内容的函数做出承诺,比如setTimeout.
假设我有:
function async(callback){
setTimeout(function(){
callback();
}, 5000);
}
async(function(){
console.log('async called back');
});
Run Code Online (Sandbox Code Playgroud)
如何创建一个async可以在setTimeout准备好后返回的承诺callback()?
我想包装它会带我到某个地方:
function setTimeoutReturnPromise(){
function promise(){}
promise.prototype.then = function() {
console.log('timed out');
};
setTimeout(function(){
return ???
},2000);
return promise;
}
Run Code Online (Sandbox Code Playgroud)
但我想不到这一点.
T.J*_*der 94
在2017年,Promises内置于JavaScript中,它们是由ES2015规范添加的(polyfill可用于过时的环境,如IE8-IE11).它们使用的语法使用您传递给Promise构造函数(Promise 执行程序)的回调,该构造函数接收用于解析/拒绝作为参数的promise的函数.
首先,因为async现在JavaScript中有一个含义(即使它在某些上下文中只是一个关键字),我将使用later该函数的名称来避免混淆.
使用本机承诺(或忠实的polyfill),它看起来像这样:
function later(delay) {
return new Promise(function(resolve) {
setTimeout(resolve, delay);
});
}
Run Code Online (Sandbox Code Playgroud)
需要注意的是,它假定的一个版本,setTimeout这是符合该定义的浏览器,其中setTimeout不传递任何参数的回调,除非你给他们的时间间隔后(这可能不是在非浏览器环境中真实的,并没有曾经是在Firefox上是真的,但现在是;它在Chrome上是真的,甚至在IE8上也是如此).
如果你希望你的函数可选地传递一个分辨率值,在任何模糊的现代浏览器上允许你setTimeout在延迟之后给出额外的参数,然后在调用时将它们传递给回调,你可以这样做(当前的Firefox和Chrome; IE11 + ,大概是Edge; 不是 IE8或IE9,不知道IE10):
function later(delay, value) {
return new Promise(function(resolve) {
setTimeout(resolve, delay, value); // Note the order, `delay` before `value`
/* Or for outdated browsers that don't support doing that:
setTimeout(function() {
resolve(value);
}, delay);
Or alternately:
setTimeout(resolve.bind(null, value), delay);
*/
});
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是ES2015 +箭头功能,则可以更简洁:
function later(delay, value) {
return new Promise(resolve => setTimeout(resolve, delay, value));
}
Run Code Online (Sandbox Code Playgroud)
甚至
const later = (delay, value) =>
new Promise(resolve => setTimeout(resolve, delay, value));
Run Code Online (Sandbox Code Playgroud)
如果您希望取消超时,则不能只返回承诺later,因为承诺无法取消.
但是我们可以使用cancel方法和promise的访问器轻松返回一个对象,并拒绝取消时的promise:
const later = (delay, value) => {
let timer = 0;
let reject = null;
const promise = new Promise((resolve, _reject) => {
reject = _reject;
timer = setTimeout(resolve, delay, value);
});
return {
get promise() { return promise; },
cancel() {
if (timer) {
clearTimeout(timer);
timer = 0;
reject();
reject = null;
}
}
};
};
Run Code Online (Sandbox Code Playgroud)
实例:
const later = (delay, value) => {
let timer = 0;
let reject = null;
const promise = new Promise((resolve, _reject) => {
reject = _reject;
timer = setTimeout(resolve, delay, value);
});
return {
get promise() { return promise; },
cancel() {
if (timer) {
clearTimeout(timer);
timer = 0;
reject();
reject = null;
}
}
};
};
const l1 = later(100, "l1");
l1.promise
.then(msg => { console.log(msg); })
.catch(() => { console.log("l1 cancelled"); });
const l2 = later(200, "l2");
l2.promise
.then(msg => { console.log(msg); })
.catch(() => { console.log("l2 cancelled"); });
setTimeout(() => {
l2.cancel();
}, 150);Run Code Online (Sandbox Code Playgroud)
通常你会有一个诺言库(你自己写的,或者其中一个).该库通常会有一个您可以创建并稍后"解析"的对象,并且该对象将具有您可以从中获得的"承诺".
然后later往往看起来像这样:
function later() {
var p = new PromiseThingy();
setTimeout(function() {
p.resolve();
}, 2000);
return p.promise(); // Note we're not returning `p` directly
}
Run Code Online (Sandbox Code Playgroud)
在对这个问题的评论中,我问:
您是否正在尝试创建自己的承诺库?
而你说
我不是,但我想现在,这实际上是我想要了解的.那个图书馆会怎么做
为了帮助理解,这里有一个非常基本的例子,它不符合Promises-A:Live Copy
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Very basic promises</title>
</head>
<body>
<script>
(function() {
// ==== Very basic promise implementation, not remotely Promises-A compliant, just a very basic example
var PromiseThingy = (function() {
// Internal - trigger a callback
function triggerCallback(callback, promise) {
try {
callback(promise.resolvedValue);
}
catch (e) {
}
}
// The internal promise constructor, we don't share this
function Promise() {
this.callbacks = [];
}
// Register a 'then' callback
Promise.prototype.then = function(callback) {
var thispromise = this;
if (!this.resolved) {
// Not resolved yet, remember the callback
this.callbacks.push(callback);
}
else {
// Resolved; trigger callback right away, but always async
setTimeout(function() {
triggerCallback(callback, thispromise);
}, 0);
}
return this;
};
// Our public constructor for PromiseThingys
function PromiseThingy() {
this.p = new Promise();
}
// Resolve our underlying promise
PromiseThingy.prototype.resolve = function(value) {
var n;
if (!this.p.resolved) {
this.p.resolved = true;
this.p.resolvedValue = value;
for (n = 0; n < this.p.callbacks.length; ++n) {
triggerCallback(this.p.callbacks[n], this.p);
}
}
};
// Get our underlying promise
PromiseThingy.prototype.promise = function() {
return this.p;
};
// Export public
return PromiseThingy;
})();
// ==== Using it
function later() {
var p = new PromiseThingy();
setTimeout(function() {
p.resolve();
}, 2000);
return p.promise(); // Note we're not returning `p` directly
}
display("Start " + Date.now());
later().then(function() {
display("Done1 " + Date.now());
}).then(function() {
display("Done2 " + Date.now());
});
function display(msg) {
var p = document.createElement('p');
p.innerHTML = String(msg);
document.body.appendChild(p);
}
})();
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
W4G*_*4G1 14
一句台词包裹着一个承诺setTimeout
await new Promise(r => setTimeout(r, ms))
Run Code Online (Sandbox Code Playgroud)
例子:
async someFunction() {
// Do something
// Wait 2 seconds
await new Promise(r => setTimeout(r, 2000))
// Do something else
}
Run Code Online (Sandbox Code Playgroud)
const setTimeoutAsync = (cb, delay) =>
new Promise((resolve) => {
setTimeout(() => {
resolve(cb());
}, delay);
});
Run Code Online (Sandbox Code Playgroud)
我们可以像这样传递自定义“cb fxn”
从 Node v15 开始,您可以使用计时器 Promise API
文档中的示例:
import { setTimeout } from 'timers/promises'
const res = await setTimeout(100, 'result')
console.log(res) // Prints 'result'
Run Code Online (Sandbox Code Playgroud)
它使用与signals浏览器非常相似的方式fetch来处理中止,请查看文档以了解更多信息:)
| 归档时间: |
|
| 查看次数: |
66521 次 |
| 最近记录: |