Git Product home page Git Product logo

iamando / asyncify Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 35 KB

This is a Go module that provides an implementation of Promises, similar to those in JavaScript, including support for then, catch, and finally. It also allows for Await to be used to block the execution of the program until the promise resolves or rejects.

Home Page: https://pkg.go.dev/github.com/iamando/asyncify

License: MIT License

Go 100.00%
async asynchronous go module package promise threading

asyncify's Introduction

Asyncify

build license Go version GoDoc

This is a Go module that provides an implementation of Promises, similar to those in JavaScript, including support for then, catch, and finally. It also allows for Await to be used to block the execution of the program until the promise resolves or rejects.

Installation

To use this module in your Go project, run the following command:

go get github.com/iamando/asyncify

Implementation

This Go module provides a simple implementation of Promises in JavaScript with support for then, catch, and finally handlers.

Usage with then, catch, and finally

Here's an example of how to use the promise type with then, catch, and finally:

package main

import "github.com/iamando/asyncify"

func main() {
  promise := asyncify.Promise(func(resolve func(interface{}), reject func(error)) {
    // Do some asynchronous operation here
    time.Sleep(2 * time.Second)

    if true {
      resolve("Success!")
    } else {
      reject(errors.New("Error!"))
    }
  })

  promise.Then(func(result interface{}) interface{} {
      // Handle fulfilled promise
      fmt.Println(result)
      return "Done!"
    }).Catch(func(err error) interface{} {
      // Handle rejected promise
      fmt.Println(err.Error())
      return "Done!"
    }).Finally(func() {
      // Handle either case
      fmt.Println("Finished!")
    })
}

In this example, a new promise is created with an asynchronous operation that takes 2 seconds to complete. The then handler is used to handle the successful resolution of the promise, the catch handler is used to handle any errors that may occur, and the finally handler is used to handle either case.

Usage with Await

Here's an example of how to use the Await method to block the execution of the program until the promise resolves or rejects:

package main

import "github.com/iamando/asyncify"

func main() {
  promise := asyncify.Promise(func(resolve func(interface{}), reject func(error)) {
    // Do some asynchronous operation here
    time.Sleep(2 * time.Second)

    if true {
      resolve("Success!")
    } else {
      reject(errors.New("Error!"))
    }
  })

  result, err := promise.Await()

  if err != nil {
    // Handle error
    fmt.Println(err.Error())
  } else {
    // Handle result
    fmt.Println(result)
  }
}

In this example, a new promise is created with an asynchronous operation that takes 2 seconds to complete. The Await method is used to block the execution of the program until the promise resolves or rejects. If the promise is resolved, the result is returned. If the promise is rejected, an error is returned.

API

type PromiseStruct

The PromiseStruct type represents a promise that will be resolved with a value or rejected with an error. It has the following methods:

Promise(executor func(resolve func(interface{}), reject func(error))) *PromiseStruct

Promise creates a new promise with an executor function that takes two functions as arguments: resolve and reject. resolve should be called with the result of the promise when it is successfully resolved, and reject should be called with an error if the promise is rejected.

Then(fn func(interface{}) interface{}) *PromiseStruct

Then creates a new promise that is resolved with the result of the fn function when the original promise is fulfilled. If the original promise is rejected, the new promise is rejected with the same error.

Catch(fn func(error) interface{}) *PromiseStruct

Catch creates a new promise that is resolved with the result of the fn function when the original promise is rejected. If the original promise is fulfilled, the new promise is resolved with the same result.

Finally(fn func()) *PromiseStruct

Finally creates a new promise

Await() (interface{}, error)

The Await function is a blocking function that allows you to wait for a promise to resolve or reject. It waits until the promise state changes from pending to either fulfilled or rejected. If the promise is fulfilled, Await returns nil, and if the promise is rejected, it returns the error that caused the rejection.

Testing

go test

Support

Asyncify is an MIT-licensed open source project. It can grow thanks to the sponsors and support.

License

Asyncify is MIT licensed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.