2.3.3 • Published 4 years ago

sync_back v2.3.3

Weekly downloads
86
License
MIT
Repository
github
Last release
4 years ago

syncBack

For the readme in English, pull down the page please.


写这个库的时候,async/await还不流行.

现在推荐大家使用async/awaitPromise来编写代码,不推荐用这个库.

出于兼容旧项目的考虑,我保留这个项目.


  • 嗯,这个库可以避免代码出现回调套回调的回调地狱(callback_hell).
  • 有人愿意帮我翻译成英文版吗?

版本

  • 这是2.x版本
  • 1.x版本请点击这里

简介

  • 形式:sync(gen, <back>)
  • 能避免回调地狱 只需要一点小技巧即可像编写同步函数一样编写流程(但实际上并不是同步执行 而是异步执行 不会阻塞)
  • 对于回调形式是(err, ...)的异步函数 当异步函数错误时 外部可以try到
  • 支持回调形式非(err, data)的异步函数
  • sync本身是一个异步的函数 如果在sync内部出现异常或执行结束 sync会以(err, data)的形式调用回调函数(如果回调函数存在)
    • 结合这一点 可以实现向外层传递异步函数的异常 请参看demo

设置

  • 生成sync函数时的设置
    • debug:若为true 则sync在执行异步函数获得错误或sync内部抛出异常时 会使用console.error输出错误或异常信息

使用

顺序执行异步函数:

var sync = require('sync_back')({ debug: true })
sync(function* (api) {
    var data = yield f1(api.next)
    console.log(data)

    var data = yield f2(api.next)
    console.log(data)
})

如果异步函数运行错误会抛出异常:

var sync = require('sync_back')({ debug: true })
sync(function* (api) {
    try {
        yield f3(api.next)
    } catch (e) {
        console.log(e)
    } finally {
        console.log('finally')
    }
    console.log('end')
})

对于回调函数非(err, data)形式的异步函数也能支持

var sync = require('sync_back')({ debug: true })
sync(function* (api) {
    var data = yield f4(function (err, data1, data2) {
        if (err)
            return api.next(err)
        api.next(null, {
            data1: data1,
            data2: data2
        })
    })

    var data = yield f5(function (data) {
        api.next(null, data)
    })
})

可以使用sync包装异步函数 使异步函数可以向外层传递异常

var sync = require('sync_back')({ debug: true })
function (back) {
    sync(function* (api) {
        ...
        throw 'err'
        ...
        back(null)
    }, back)
}

sync(function* (api) {
    ...
    try {
        yield f(api.next)
    } catch (e) {
        ...
    }
    ...
})

更多例子在demo文件夹中,可直接运行.

兼容性

生成器函数yield关键字都是ES6标准的特性.所以只能用在支持ES6的环境中.

问题

  • 为什么不用co?
    • ...
  • 为什么不用promise?
    • ...
  • 为什么不用RxJS?
    • ...
  • 为什么不用$%^&*?
    • 对不起我错了...我还是喜欢这样的写法...
  • 还会更新吗?
    • 有bug请反馈 会尽力修
    • 有其他需求或建议(例如类似promise.all的运行方式)的话 请反馈 有空会考虑加
    • 欢迎pullRequest

At the time of writing this library, async/await was not yet popular.

Now it is recommended that you use async/await and Promise to write code, and it is not recommended to use this library.

For compatibility with the old project, I keep this project.


My English is not very good.

These are machine translations.Is anyone willing to help me optimize this translation?

  • Well, this library can avoid the callback_hell in the code callback callback.

Version

  • This is version 2.x
  • Version 1.x click here

Introduction

  • sync(gen, <back>)
  • Avoid callback hell and write your logic like writing synchronous code.(But it doesn't block the code from running, so it can also enjoy the advantages of asynchronous)
  • For asynchronous functions whose callback form is (err, ...), when the asynchronous function is wrong, the external can use try
  • Supports callbacks in asynchronous functions that are not (err, data)
  • sync is itself an asynchronous function. If an exception occurs inside sync, or if sync ends normally, sync will call the callback function as (err, data) (if the callback function exists)
    • In conjunction with this, you can pass an exception of the asynchronous function to the outside, see demo

Set

  • Setting when generating sync function
    • debug: If true then sync will use console.error to output error or exception information when executing an asynchronous function get error or sync internally throws an exception.

Use

Execute asynchronous functions sequentially:

var sync = require('sync_back')({ debug: true })
sync(function* (api) {
    var data = yield f1(api.next)
    console.log(data)

    var data = yield f2(api.next)
    console.log(data)
})

Asynchronous function throws an exception when it returns an error:

var sync = require('sync_back')({ debug: true })
sync(function* (api) {
    try {
        yield f3(api.next)
    } catch (e) {
        console.log(e)
    } finally {
        console.log('finally')
    }
    console.log('end')
})

Also supports asynchronous functions in the form of callback functions other than (err, data)

var sync = require('sync_back')({ debug: true })
sync(function* (api) {
    var data = yield f4(function (err, data1, data2) {
        if (err)
            return api.next(err)
        api.next(null, {
            data1: data1,
            data2: data2
        })
    })

    var data = yield f5(function (data) {
        api.next(null, data)
    })
})

By using the sync wrapper async function, you can make an async function pass an exception to the outer layer

var sync = require('sync_back')({ debug: true })
function (back) {
    sync(function* (api) {
        ...
        throw 'err'
        ...
        back(null)
    }, back)
}

sync(function* (api) {
    ...
    try {
        yield f(api.next)
    } catch (e) {
        ...
    }
    ...
})

The examples that can be run are in the demo folder.

Compatibility

The generator function and yield keywords are all features of the ES6 standard, so they can only be used in environments that support ES6.

Problem

  • Why not to use Co?
    • Well…
  • Why no to use Promise?
    • Err…
  • Why not to use RxJS?
    • Hehe…
  • Why not to use blablabla…?
    • Okay, that my mistake… I like this style.
  • Will it be updated in the future?
    • If there is a bug, I will try my best
    • If there are other needs or suggestions (such as the operation of promise.all), please give me feedback.
    • Welcome to pullRequest
2.3.3

4 years ago

2.3.2

6 years ago

2.3.1

6 years ago

2.3.0

6 years ago

2.2.8

6 years ago

2.2.7

6 years ago

2.2.6

6 years ago

2.2.5

6 years ago

2.2.4

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago