Comments (2)
Also
- add an example to show users that every function body requires another call to asyncblock before calling any async functions and that passing the flow object to callee functions should not be allowed;
- add documentation that asyncblock.enableTransform() causes Node.js Tools debugger for Visual Studio 2015 to be unable to place breakpoints or single step because the final code is transformed and executed somewhere else. A workaround for the enableTransform() and .sync() convenience transform API is to not use it and use the comparable flow.sync() API instead.
By the way, Asyncblock is one of the most useful modules in Node.js and the best Fiber wrapper libraries out there. It's amazing why Node.js hasn't added Asyncblock and Fiber as part of its core deliverable. Writing asynchronous code couldn't be more easier and cleaner. Thank you.
from asyncblock.
On the surface async/await looks like it could replace asyncblock, but it cannot until JavaScript also exposes a parent async context like asyncblock. There's no way to call await from a callback because of this. Here is a non-working async/await JavaScript ES7 example:
`
// An example showing how async/await cannot be used from a callback context.
//
// Using experimental Node.js v7.0.0:
// node --harmony test_asyncawait_bad.js
//
// Depends on:
// npm install request-promise
// npm install request
// npm install cheerio
//
"use strict";
let request = require("request-promise");
let cheerio = require("cheerio");
async function main () {
try {
let firstPage = await request.get("http://www.example.com");
let $ = cheerio.load(firstPage);
let hrefNodes = $("a");
hrefNodes.each((i, e) => {
let linkFound = $(e).attr("href");
let secondPage = await request.get(linkFound);
console.log("Contents of link found: " + secondPage);
});
} catch (e) {
console.log("ERROR: " + e.stack);
}
}
main();
/*
OUTPUT:
let secondPage = await request.get(linkFound);
^^^^^^^
SyntaxError: Unexpected identifier
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:545:28)
at Object.Module._extensions..js (module.js:582:10)
at Module.load (module.js:490:32)
at tryModuleLoad (module.js:449:12)
at Function.Module._load (module.js:441:3)
at Module.runMain (module.js:607:10)
at run (bootstrap_node.js:382:7)
at startup (bootstrap_node.js:137:9)
at bootstrap_node.js:497:3
*/
`
Here is a working asyncblock example that does what the async/await could not do:
`
// An example showing how Asyncblock can be used from a callback context.
//
// Using Node.js v6.9.1:
// node --harmony test_asyncblock_good.js
//
// Depends on:
// npm install asyncblock
// npm install request
// npm install cheerio
//
"use strict";
let asyncblock = require("asyncblock");
let request = require("request");
let cheerio = require("cheerio");
asyncblock(flow => {
try {
let firstPage = flow.sync(["response"], request, "http://www.example.com");
let $ = cheerio.load(firstPage.response.body);
let hrefNodes = $("a");
hrefNodes.each((i, e) => {
let linkFound = $(e).attr("href");
let secondPage = flow.sync(["response"], request, linkFound);
console.log("Contents of link found: " + secondPage.response.body);
});
} catch (e) {
console.log("ERROR: " + e.stack);
}
});
`
As a bonus here is a working asyncblock example showing that the asynchronous context can be re-used and it makes code look real nice. This is what I wanted from JavaScript ES7 but that seems to remain a limitation.
`
// An example showing how to use Asyncblock's get current flow context from
// another function.
//
// Using Node.js v6.9.1:
// node --harmony test_asyncblock_good2.js
//
// Depends on:
// npm install asyncblock
// npm install request
// npm install cheerio
//
"use strict";
let asyncblock = require("asyncblock");
let request = require("request");
let cheerio = require("cheerio");
asyncblock(flow => {
try {
let $ = cheerio.load(fetchPage("http://www.example.com"));
let hrefNodes = $("a");
hrefNodes.each((i, e) => {
let linkFound = $(e).attr("href");
console.log("Contents of link found: " + fetchPage(linkFound));
});
} catch (e) {
console.log("ERROR: " + e.stack);
}
});
function fetchPage (url) {
let flow = asyncblock.getCurrentFlow();
let result = flow.sync(["response"], request, url);
return (typeof result.response !== "undefined" && result.response !== null) ? result.response.body : "";
}
`
There is just no replacement for asyncblock to date. It is so handy.
from asyncblock.
Related Issues (20)
- x().sync() optimization
- Windows line numbers off on source transformation HOT 1
- Convert shell scripts to node scripts for cross platform-ness HOT 1
- asyncblock.enableTransform() doesnt working HOT 6
- storing deferred results in a hash HOT 1
- Switch parser to acorn HOT 1
- Transform breaks on "comment" object key HOT 1
- Parse error on require('x' + x) HOT 1
- undefined return value HOT 1
- "use strict" throws Fatal error HOT 3
- Is it possible to use asyncblock with the Q promise library? HOT 3
- some codes can't work with source transformation HOT 3
- Won't install on ubuntu (and all other debians i guess) until /usr/bin/nodejs linked to /usr/bin/node HOT 2
- Dependency (fibers) is "gone forever" HOT 1
- No fibers found... HOT 2
- How to emulate a simple blocking call? HOT 3
- enabling Transforms in exported module HOT 2
- interactive? HOT 3
- When we will see updates?? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asyncblock.