asyncify

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 0 Imported by: 0

README

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/goify/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/goify/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/goify/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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PromiseState

type PromiseState int

type PromiseStruct

type PromiseStruct struct {
	// contains filtered or unexported fields
}

func Promise

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

Promise creates and returns a new Promise object that represents the eventual completion (or failure) of an asynchronous operation, and executes the specified executor function immediately. The executor function takes two arguments, a resolve function and a reject function, that allow the promise to be resolved with a value or rejected with a reason, respectively. The Promise object returned by this function has methods `Then`, `Catch`, and `Finally` that allow for chaining and handling of the eventual fulfillment or rejection of the promise. The Promise object also provides a blocking `Await` method that can be used to wait for the promise to be resolved or rejected.

func (*PromiseStruct) Await

func (promise *PromiseStruct) Await() (interface{}, error)

Await blocks the execution of the program until the promise resolves or rejects, and returns either the resolved value or an error. It returns an error only if the promise was rejected, and the resolved value otherwise.

func (*PromiseStruct) Catch

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

Catch registers a callback function to be called when the promise is rejected. If the promise is already rejected, the callback is called immediately with the rejection reason. If the promise is resolved, the callback is skipped. The callback function takes one argument, the rejection reason of the promise, and should return a value or a new promise that will be resolved with that value. Returns a new promise that is resolved with the return value of the callback function or rejected with the same reason as the original promise, if the callback function throws an error.

func (*PromiseStruct) Finally

func (promise *PromiseStruct) Finally(fn func()) *PromiseStruct

Finally registers a callback function to be called when the promise is either resolved or rejected. If the promise is already resolved or rejected, the callback is called immediately. The callback function takes no arguments and should not return anything. Returns the same promise instance to allow for chaining of methods.

func (*PromiseStruct) Then

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

Then registers a callback function to be called when the promise is resolved. If the promise is already resolved, the callback is called immediately with the resolved value. If the promise is rejected, the callback is skipped. The callback function takes one argument, the resolved value of the promise, and should return a value or a new promise that will be resolved with that value. Returns a new promise that is resolved with the return value of the callback function or rejected with the same reason as the original promise, if the callback function throws an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL