# waitfor-ES6 **Repository Path**: mirrors_GerHobbelt/waitfor-ES6 ## Basic Information - **Project Name**: waitfor-ES6 - **Description**: Sequential programming for node.js -and the browser-. End of callback hell - Original Wait.for, implemented using upcoming javascript/ES6-Harmony generators. - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-19 - **Last Updated**: 2026-05-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Wait.for-ES6 =========== Sequential programming for node.js *and the browser*, end of callback hell. ***Simple, straightforward abstraction.*** By using **wait.for**, you can call any nodejs standard async function in sequential/Sync mode, waiting for result data, without blocking node's event loop. Definitions: -- * A nodejs standard async function is a function in which the last parameter is a callback: function(err,data) * A "fiber" in this context is a "generator" that yields async callable functions. *Advantages:* - WARNING: Bleeding Edge - -- This is a port of the original [Wait.for] (http://github.com/luciotato/waitfor), now implemented using ***the upcoming*** javascript/ES6-Harmony generators. It requires ***bleeding edge node v0.11.6, with --harmony command line option*** This lib is based on ECMAScript 6 "Harmony", the next version of the javascript standard, target release date December 2013. This lib also uses bleeding edge V8 Harmony features, so you’ll need to use the latest (unstable) nodejs version (v0.11.6) and also pass the --harmony flag when executing node. Example: cd samples/blogServer node --harmony server.js Wait.for on stable Node -- If you want to use ***wait.for*** but you can't use (unstable) node and/or ES6-Harmony you can try the
[Wait.for version based on node-fibers] (http://github.com/luciotato/waitfor), which only requires node >= 0.5.2, and the stable package [node-fibers](https://github.com/laverdet/node-fibers) Install: - npm install wait.for-es6 Examples: - ```javascript // (inside a generator) call async function fs.readfile(path,enconding), // wait for result, return data console.log('contents of file: ', yield wait.for(fs.readfile, '/etc/file.txt', 'utf8')); ``` DNS testing, *using pure node.js* (a little of callback hell): ```javascript var dns = require("dns"); function test(){ dns.resolve4("google.com", function(err, addresses) { if (err) throw err; for (var i = 0; i < addresses.length; i++) { var a = addresses[i]; dns.reverse(a, function (err, data) { if (err) throw err; console.log("reverse for " + a + ": " + JSON.stringify(data)); }); }; }); } test(); ``` ***THE SAME CODE***, using **wait.for** (sequential): ```javascript var dns = require("dns"), wait=require('wait.for-es6'); function* test(){ var addresses = yield wait.for(dns.resolve4,"google.com"); for (var i = 0; i < addresses.length; i++) { var a = addresses[i]; console.log("reverse for " + a + ": " + JSON.stringify( yield wait.for(dns.reverse,a))); } } wait.launchFiber(test); ``` Alternative, **fancy syntax**, *omiting* **wait.for** (see [The funny thing is...](#the-funny-thing-is)) ```javascript var dns = require("dns"), wait=require('wait.for-es6'); function* test(){ var addresses = yield [dns.resolve4, "google.com"]; for( let i=0; iSurprisingly, ES6 generators-based implementation of function wait.for(asyncFn) is almost a no-op, you can even omit it... Given that evaluating ***wait.for*** return its arguments, the call can be replaced with an object literal, which is an array-like object. It results that: ```javascript wait.for( asyncFn, arg1, arg2 ) // return arguments === {0:asyncFn, 1:arg1, 2:arg2 } // is equivalent to... ~= [ asyncFn, arg1, arg2 ] // is similar to... ``` so, the following two snippets are equivalent (inside a generator launched via ***wait.launchFiber(generator)***): ```javascript // call an async function and wait for results, (wait.for syntax): console.log( yield wait.for ( fs.readFile, '/etc/somefile', 'utf8' ) ); // call an async function and wait for results, (fancy syntax): console.log( yield [ fs.readFile, '/etc/passwd', 'utf8' ] ); ``` Roadmap -- * Parallel execution, launch one fiber for each array item, waits until all fibers complete execution. * **function parallel.map(arr,fn,callback)** return transformed array; * **function parallel.filter(arr,fn,callback)** return filtered array; * Status: *BETA* in complementary lib [parallel.js](http://github.com/luciotato/parallel-ES6) Related -- I've ported this functionality to [LiteScript](//github.com/luciotato/LiteScript) - LiteScript is a -beta-stage- higly readable, compile to js language. LiteScript has type annotations, a compile-time validation phase, and catch common js errors and typos in object property names, speeding up development (you code faster) and saving hours of debugging over a mistyped property name. [Try LiteScript online](http://luciotato.github.io/LiteScript_online_playground/playground) Here it is a sample of LiteScript Code, showing "yield until" (wait for async to complete) and "yield parallel" (launch in parallel, wait until all asyncs complete) #####get google.com IPs, then reverse DNS (in parallel) global import dns, nicegen nice function resolveAndParallelReverse try var addresses:array = yield until dns.resolve "google.com" var results = yield parallel map addresses dns.reverse for each index,addr in addresses print "#{addr} reverse: #{results[index]}" catch err print "caught:", err.stack end nice function ---------------