Simple Sync is a simple sync function-wrapper for node js, based on fibers module.
来源:互联网 发布:玉溪明升软件 编辑:程序博客网 时间:2024/05/06 22:40
https://www.npmjs.com/package/simplesync
Simple Sync is a simple sync function-wrapper for node js, based on fibers module.
How to install:
npm install simplesync
How to use:
//First you need to require simplesync modulevar sync = require('simplesync');//Any sync call must be in a sync block.sync.block(function() {//Standard async callSome_Async_Function(123, function(ta) {//ta will be 123.});//We name the first parameter of callback to "ta"//If there is multi parameters in callback function, just using like this://sync.cb("ta1", "ta2", "ta3");//var result = sync.wait(Some_Async_Function(123, sync.cb("ta")));//Now we can use the "ta" in result. This will print 123.//console.log("TA:= " + result.ta);//And you can wait for multi functions finished, like this:result = sync.wait(function() {Some_Async_Function(123, sync.cb("ta"));Some_Async_Function2(123, sync.cb("ta", "ta2"));});////result[0].ta = 123;//result[1].ta = 124;//result[1].ta2 = 678;}//Sample async function. It is no matter about this function is in or not in the sync block.function Some_Async_Function(input, callback) {callback(input);}function Some_Async_Function2(input, callback) {callback(input + 1, 678);}
Chinese document: 支持单个函数异步转同步调用,或者一组函数并行运行后转同步调用。
如何使用: //首先引入模块, var sync = require('simplesync');
//任何同步调用都必须位于一个sync.block块内, sync.block描述可以嵌套。sync.block(function() { //标准异步调用方式 Some_Async_Function(123, function(ta) { //这里参数ta的值是124 }); //这是同步调用方式, result.ta就是124 var result = sync.wait(Some_Async_Function(123, sync.cb("ta"))); //多个参数就这么办 function Async_Function2(input, callback) { callback(input, 456); } //这里result.ta就是123, result.ta2就是456了 result = sync.wait(Async_Function2(123, sync.cb("ta", "ta2")));//当需要等待多个函数并行运行后统一结束的,用如下的方式:result = sync.wait(function() {Some_Async_Function(123, sync.cb("ta"));Some_Async_Function2(123, sync.cb("ta", "ta2"));});////result[0].ta = 123;//result[1].ta = 124;//result[1].ta2 = 678; }//异步调用函数可以在sync.block内,也可以不在,无所谓。function Some_Async_Function(input, callback) { callback(input+1);}function Some_Async_Function2(input, callback) {callback(input + 1, 678);}
0 0
- Simple Sync is a simple sync function-wrapper for node js, based on fibers module.
- A simple kernel module for linux
- a simple function for memory dumping
- A Simple C# Wrapper for the AviFile Library
- JavaScript Debug: A simple wrapper for console.log
- Create a simple module
- A Simple MVC Setup In Node.JS
- A simple echo client & server based on twisted framework
- Creating a simple web prototype based on CppCMS
- A simple maze function
- a simple GP based C++
- A Simple Windows HTTP Wrapper Using C++
- A Simple Win32 Window Wrapper Class
- A Simple For Emberjs
- A Simple Web Service In Node.Js + CouchDB
- Implement a simple iterator using javascript (node.js)
- Appendix: Is there a simple algorithm for intelligence?
- Simple Mixer Control Wrapper
- Leetcode-14. Longest Common Prefix
- Matlab画图如何去掉图形框右边和上边的刻度
- Java基础知识-计算机基础
- 抽象类的使用技巧
- [每天学习一点点] 查询表关联
- Simple Sync is a simple sync function-wrapper for node js, based on fibers module.
- java基础_day0017_数组_最大值_成绩_二维数组
- JAVA进程高CPU占用故障排查
- MVC 缓存
- Android Studio 自动导入明确的唯一包
- JSPatch被停用了,也就是不能使用JSPatch第三方框架热修复了
- php 知识杂记(持续更新中)
- C++ 网络编程处理数据断续
- js利用offsetWidth和clientWidth来计算滚动条的宽度