New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/demoinmuxi/async

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/demoinmuxi/async

  • v0.0.0-20220417005815-dde9112ce7ff
  • Source
  • Go
  • Socket score

Version published
Created
Source

async

一个模仿javascript中的async和promise的库,以同步的方式书写并发代码

async.Do()

这个函数接受一个想要异步调用的函数asyncTask和它的参数,返回一个含有Await方法的结构体,Await方法可以用来阻塞调用该方法的函数执行并一直等待到asyncTask执行完成,并返回asyncTask函数的返回结果

async.NewPromise()

这个函数接受一个想要异步调用的函数promiseTask,在调用时会传入resolve和reject方法用来控制该promise的状态,状态一旦settled(resolved or rejected)便不可以再改变,返回一个*async.Promise结构体 ####Promise

Await()

等待promise settled 并返回结果

Then()

promise settled 后的回调函数 返回*Promise

Catch()

promise rejected 后的回调函数 返回*Promise

Finally()

promise settled后一定会执行的函数 返回*Promise

async.Wait()

阻塞调用该方法的函数并等待使用async包发起的所有asyncTask和promiseTask的完成

func action(params ...interface{}) interface{} { a, b := params[0].(int), params[1].(string) time.Sleep(time.Second * 2) fmt.Println(a, b) return 100 } func promise(resolve, reject async.Handler) { fmt.Println("promise start!!!") time.Sleep(time.Second * 2) resolve(100) fmt.Println("promise end!!!") reject(-100) } func Then(i interface{}) interface{} { fmt.Println(i) return i.(int) + 1 } func main() { task := async.Do(action, 10, "hi~") async.Do(action, 20, "ha~") c := async.NewPromise(promise) fmt.Println(c.Then(Then).Then(Then,Then).Finally(func() { fmt.Println("promise finally") }).Await()) fmt.Println(task.Await()) async.Wait() }

输出结果

promise start!!!
10 hi~
promise end!!!
100
101
promise finally
102
100
20 ha~

FAQs

Package last updated on 17 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc