Skip to content

Latest commit

 

History

History
82 lines (54 loc) · 2.01 KB

README.md

File metadata and controls

82 lines (54 loc) · 2.01 KB

Async API for Scala.js

async - Higher-order functions and common patterns for asynchronous code.

Description

Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with Node.js and installable via npm install --save async, it can also be used directly in the browser.

Build Dependencies

Build/publish the SDK locally

 $ sbt clean publish-local

Running the tests

Before running the tests the first time, you must ensure the npm packages are installed:

$ npm install

Then you can run the tests:

$ sbt test

Examples

Using Async asynchronously via callbacks

import io.scalajs.npm.async.Async
import scala.scalajs.js


// create a queue object with concurrency 2
val q = Async.queue[Task]((task: Task, callback: js.Function0[Any]) => {
    println("hello " + task.name)
    callback()
}, 2)

// assign a callback
q.drain = () => println("all items have been processed")

// add some items to the queue
q.push(new Task(name = "foo"), (err: Error) => println("finished processing foo"))

q.push(new Task(name = "bar"), (err: Error) => println("finished processing bar"))

// add some items to the queue (batch-wise)
q.push(js.Array(new Task(name = "baz"), new Task(name = "bay"), new Task(name = "bax")), (err: Error) => {
  println("finished processing item")
})

// add some items to the front of the queue
q.unshift(new Task(name = "bar"), (err: Error) => println("finished processing bar"))


class Task(val name: String) extends js.Object

Artifacts and Resolvers

To add the Async binding to your project, add the following to your build.sbt:

libraryDependencies += "io.scalajs.npm" %%% "async" % "0.5.0"

Optionally, you may add the Sonatype Repository resolver:

resolvers += Resolver.sonatypeRepo("releases")