We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
关键词:promise.then 与 setTimeout 并行
抓住 promise 的状态只能由 pending -> fulfilled,或者 pending -> rejected,并且只能进行一次改变
function promiseWithTimeout(url, timeout = 3000) { return new Promise((resolve, reject) => { fetch(url).then(data => data.json()).then(data => resolve(data)); // fetch 先得到结果就 resolve setTimeout(() => reject(Error('time is out!')), timeout); // 时间到了还没 fetch 到就 reject }); } promiseWithTimeout('http://localhost:8080/data.json') .then(data => console.log(data)) .catch(err => console.error(err)); // server.js 测试 const express = require('express'); const cors = require('cors'); const app = express(); app.use(cors({ origin: '*' })); app.use('/data.json', (req, res) => { setTimeout(() => res.end(JSON.stringify({ a: 1 })), Math.floor(Math.random() * 6 * 1000)); }); app.listen(8080, () => console.log('the app is running at http://localhost:8080'));
The text was updated successfully, but these errors were encountered:
也可以使用Promise.race
Sorry, something went wrong.
@DualWield 是呀,本质就是在问 race 实现
No branches or pull requests
用 promise 实现一个请求超时功能
关键词:promise.then 与 setTimeout 并行
抓住 promise 的状态只能由 pending -> fulfilled,或者 pending -> rejected,并且只能进行一次改变
The text was updated successfully, but these errors were encountered: