[Typescript] Promise based delay function using async / await
2018-01-24 20:09
609 查看
Learn how to write a promise based delay function and then use it in
Lets say you want to call a function after 1s, 2s, 3s. You can use
async awaitto see how much it simplifies code over
setTimeout.
Lets say you want to call a function after 1s, 2s, 3s. You can use
setTimeout, or you can wrap it up into a simple delay function that works with async/await
const delay = (ms) => new Promise(res => setTimeout(res, ms)); const runAsync = async (cb) => { await delay(100); cb('1s') await delay(1000); cb('2s') await delay(1000); cb('3s') } runAsync((m) => { console.log(m)})
相关文章推荐
- [TypeScript] Simplify asynchronous callback functions using async/await
- 【前端前沿看点】告别promise的低可读性,Async/Await替代Promise的6个理由
- callback, promise, co/yield, async/await 大混战
- using INSTR function for obtaining data from a table, based on a selection from the master table (FK relation)
- 重构:从Promise到Async/Await
- Callback Promise Generator Async-Await 和异常处理的演进_2
- Node.js异步控制流:回调、事件、Promise和async/await
- async await promise
- 使用Typescript写的Vue初学者Hello World实例(实现按需加载、跨域调试、await/async)
- 用async-await实现类似Promise.all()的执行效果
- C# Async, Await and using statements
- USING ES7 ASYNC/AWAIT TODAY WITH BABEL
- nodejs-typescipt-Promise代码实例讲解,看完就理解async和await了
- ES6/7/8新特性Promise,async,await,fetch带我们逃离异步回调的深渊
- async 和await的结合promise用法
- [Ramda] Refactor a Promise Chain to Function Composition using Ramda
- Async/Await替代Promise的6个理由
- 关于Promise,Generator,async / await 对异步的处理
- 理解异步之美:Promise 与 async await(二)
- sleep()在ES7中利用Promise和async/await的优雅实现