Holds function executions back until a certain requirement is met
A "whenthen" holds function executions back until it has been resolved by a (asynchronous) "worker" function and optionally passes arguments to all the held-back functions. Once the "whenthen" has been resolved all functions will be executed immediately, even those added after it's been resolved.
It is particularly useful when functions have to wait until something happened, or wait for data to become available.
npm install whenthen
Returns a new object that can be used to hold back functions until the worker has resolved. If provided, the
worker will be executed the first time a function is added using
worker will receive a function
resolve as its only argument that can be used to resolve the "whenthen". All arguments passed to
resolve will be passed on to every task function.
Adds a task function to the "whenthen". The function will be held back until the "whenthen" has been resolved.
Resolves the "whenthen". All arguments passed passed to
.resolve will be passed on to every task function.
This can be used as an alternavie to passing a worker function to
Run the functions
doMoreStuff only after
fakeAsyncLogin is done:
var whenthen = require'whenthen';var whenLoggedIn = whenthensetTimeoutconsole.log'Logged in'resolve;1000;;whenLoggedIndoconsole.log'Doing stuff after logged in';;whenLoggedIndoconsole.log'Doing more stuff after logged in';;
fakeFetchSomeData has some data:
var whenthen = require'whenthen';var whenDataReady = whenthensetTimeoutconsole.log'Fetched some data';resolvenull x: 100 y: 200;2000;;whenDataReadydoif err return console.log'O-oh' err;console.log'X is' datax;;whenDataReadydoif err return console.log'O-oh' err;console.log'Y is' datay;;
Alternatively resolve then whenthen from outside using
var whenKittensBorn = whenthen;whenKittensBorndoconsole.log'Kittens born' date;;console.log'Waiting for kittens birth';setTimeoutwhenKittensBornresolveDatenow;3000;
Copyright (c) 2012 Max Kueng
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.