waitfor-ES6

所属分类:Python编程
开发工具:JavaScript
文件大小:0KB
下载次数:0
上传日期:2016-01-23 16:41:22
上 传 者sh-1993
说明:  node.js-和浏览器的顺序编程-。回调地狱结束-原始Wait.for,使用upc实现...
(Sequential programming for node.js -and the browser-. End of callback hell - Original Wait.for, implemented using upcoming javascript/ES6-Harmony generators.)

文件列表:
LICENSE (1080, 2016-01-23)
package.json (1104, 2016-01-23)
samples/ (0, 2016-01-23)
samples/ajaxServer/ (0, 2016-01-23)
samples/ajaxServer/client.js (2551, 2016-01-23)
samples/ajaxServer/page.html (174, 2016-01-23)
samples/ajaxServer/run (23, 2016-01-23)
samples/ajaxServer/server.js (2288, 2016-01-23)
samples/blogServer/ (0, 2016-01-23)
samples/blogServer/blogPost.txt (429, 2016-01-23)
samples/blogServer/blogTemplate.html (100, 2016-01-23)
samples/blogServer/run (74, 2016-01-23)
samples/blogServer/server.js (3070, 2016-01-23)
samples/blogServer/style.css (239, 2016-01-23)
samples/uglifydir/ (0, 2016-01-23)
samples/uglifydir/fsUtil.js (3612, 2016-01-23)
samples/uglifydir/run.sh (36, 2016-01-23)
samples/uglifydir/sequential-exec.js (2479, 2016-01-23)
samples/uglifydir/test/ (0, 2016-01-23)
samples/uglifydir/test/ASTBase.js (16166, 2016-01-23)
samples/uglifydir/test/Args.js (1288, 2016-01-23)
samples/uglifydir/test/Compiler.js (14950, 2016-01-23)
samples/uglifydir/test/Environment.js (6379, 2016-01-23)
samples/uglifydir/test/Grammar.js (51787, 2016-01-23)
samples/uglifydir/test/Lexer.js (20134, 2016-01-23)
samples/uglifydir/test/NameDeclaration.js (7308, 2016-01-23)
samples/uglifydir/test/Producer_js.js (26651, 2016-01-23)
samples/uglifydir/test/SourceMap.js (4529, 2016-01-23)
samples/uglifydir/test/Validate.js (38729, 2016-01-23)
samples/uglifydir/test/fsUtil.js (3612, 2016-01-23)
samples/uglifydir/test/lite (90, 2016-01-23)
samples/uglifydir/test/lite-cli.js (6427, 2016-01-23)
samples/uglifydir/test/log.js (3207, 2016-01-23)
samples/uglifydir/test/mkPath.js (865, 2016-01-23)
samples/uglifydir/test/string-shims.js (4810, 2016-01-23)
samples/uglifydir/test/ug.sh (102, 2016-01-23)
test/ (0, 2016-01-23)
test/test.js (2980, 2016-01-23)
... ...

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:*
  • Avoid callback hell / pyramid of doom
  • Simpler, sequential programming when required, without blocking node's event loop
  • Simpler, try-catch exception programming. (default callback handler is: if (err) throw err; else return data)
  • You can launch multiple parallel non-concurrent fibers.
  • No multi-threaded debugging nightmares, only one fiber running at a given time.
  • Can use any node-standard async function with callback(err,data) as last parameter.
  • Plays along with node programming style. Write your async functions with callback(err,data), but use them in sequential/SYNC mode when required.
  • Plays along with node cluster. You design for one thread/processor, then scale with cluster on multicores.
- 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 ---------------

近期下载者

相关文件


收藏者