@mybug/wait
Advanced tools
Comparing version 0.1.1 to 0.1.2
10
index.js
@@ -1,4 +0,5 @@ | ||
let loop = require('deasync').runLoopOnce | ||
module.exports = wait | ||
const deasync = require('deasync') | ||
module.exports = function (awaitable) { | ||
function wait (awaitable) { | ||
let result | ||
@@ -17,6 +18,3 @@ let error | ||
}) | ||
// eslint-disable-next-line | ||
while (!done) { | ||
loop() | ||
} | ||
deasync.loopWhile(() => !done) | ||
if (error) { | ||
@@ -23,0 +21,0 @@ throw error |
{ | ||
"name": "@mybug/wait", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "await anything synchronously", | ||
@@ -18,2 +18,7 @@ "main": "index.js", | ||
"await", | ||
"awaitor", | ||
"awaitable", | ||
"waitable", | ||
"callback", | ||
"thunk", | ||
"mybug", | ||
@@ -20,0 +25,0 @@ "promise", |
@@ -7,26 +7,46 @@ # wait | ||
params | ||
return | ||
params awaitable: an awaitable value | ||
return the promiseValue just like using await | ||
## example | ||
```javascript | ||
const wait = require('@mybug/wait') | ||
const su = require('superagent') | ||
let resp = wait(su('https://api.npms.io/v2/search?q=scope:mybug')) | ||
console.log(resp.body) | ||
let resp = wait(su('https://api.npms.io/v2/search?q=scope:mybug')) //async http request | ||
console.log(resp.body) // result is printed synchronously | ||
``` | ||
## example | ||
## use together with @mybug/awaitor | ||
wait can resolve an operation which returns a promise, but not an operation which needs a callback. | ||
you can either: | ||
1. wrap the operation to return a promise | ||
2. just call the operation, use @mybug/awaitor as callback, like below | ||
```javascript | ||
const awaitor = require('@mybug/awaitor') | ||
const wait = require('@mybug/wait') | ||
const su = require('superagent') | ||
let resp = wait(su('https://api.npms.io/v2/search?q=scope:mybug')) | ||
console.log(resp.body) | ||
let cb = awaitor() | ||
foo(1,2,cb) // whenever a callback is needed, just use cb | ||
let result = wait(cb) //result is an array, containing the args cb received | ||
console.log(result) // [3] | ||
function foo(a,b,cb){ | ||
setTimeout(()=>cb(a+b),1000) | ||
} | ||
``` | ||
##also useful as debug tool | ||
When stopping on a breakpoint, you can evaluate expressions but no result of async operations can be evaluated | ||
## also useful as debug tool | ||
When being paused on a breakpoint, you can evaluate expressions but no async operations can be evaluated | ||
immediately. | ||
Use @mybug/wait to solve the problem by simply evaluate `wait(asyncOp())` | ||
Use @mybug/wait to solve the problem, you will get the result of the async operation while still being paused on that breakpoint. | ||
You don't even have to require @mybug/wait in your code. Just install @mybug/wait as a devDependency. When debugging, | ||
evaluate `global.wait=process.mainModule.require('@mybug/wait')`. Then by evaluating `wait(anyAsyncOperation())`, | ||
you will see the | ||
async operation gets done and returned. |
4308
52
23