async - Higher-order functions and common patterns for asynchronous code.
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.
$ sbt clean publish-local
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
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
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")