1.0.1 • Published 7 years ago
rala v1.0.1
What's this?
Converts functions which has callback function like setTimeout
to awaitable function.
- If callback function is located in the first, you can use
la
. - If callback function is located in the last, you can use
ra
.
Examples
Simple case
const rala = require("rala")
!async function(){
await rala.la(setTimeout, 2000)
console.log("This message will be shown after 2 sec after.")
}()
Install
install
function extends built-in functions and prototypes.
- Promise.left
- Promise.right
- Function.prototype.la
- Function.prototype.ra
require("rala").install()
function sleepL(callback, time){
const start = Date.now()
setTimeout(_ => {
callback(Date.now() - start)
}, time)
}
function sleepR(time, callback){
const start = Date.now()
setTimeout(_ => {
callback(Date.now() - start)
}, time)
}
!async function(){
console.time("1000ms")
const actual_wait1 = await sleepL.la(1000)
console.timeEnd("1000ms")
console.log(actual_wait1)
console.time("2000ms")
const actual_wait2 = await sleepR.ra(2000)
console.timeEnd("2000ms")
console.log(actual_wait2)
console.time("foo")
const value = await Promise.left(setTimeout)(1000, "foo")
console.timeEnd("foo")
console.log(value)
}()
// 1000ms: 1.00e+3ms
// 1001
// 2000ms: 2.00e+3ms
// 2002
// foo: 1.00e+3ms
// foo
Chrome Extensions
Almost functions of chrome extensions has callback function at last.
!async function(){
const tabs = await chrome.tabs.query.ra({})
console.log(tab)
}()
Then
Of course you can also use then
method.
setTimeout.la(2000).then(_ => console.log("2 seconds passed."))