https://github.com/scalajs-io/mpromise
Promises A+ conformant implementation
https://github.com/scalajs-io/mpromise
mongodb mongoose node node-js node-module nodejs npm npm-module npm-package promise promise-library promises scala scalajs
Last synced: 2 months ago
JSON representation
Promises A+ conformant implementation
- Host: GitHub
- URL: https://github.com/scalajs-io/mpromise
- Owner: scalajs-io
- License: apache-2.0
- Created: 2017-02-25T18:53:07.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-06-17T23:10:15.000Z (almost 7 years ago)
- Last Synced: 2025-01-17T22:42:31.743Z (over 1 year ago)
- Topics: mongodb, mongoose, node, node-js, node-module, nodejs, npm, npm-module, npm-package, promise, promise-library, promises, scala, scalajs
- Language: Scala
- Size: 15.6 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mpromise binding for Scala.js
================================
[mpromise](https://www.npmjs.com/package/mpromise) - A promises/A+ conformant implementation, written for mongoose.
### Description
A promises/A+ conformant implementation, written for [mongoose](https://github.com/scalajs-io/mongoose).
### Build Dependencies
* [SBT v1.2.x](http://www.scala-sbt.org/download.html)
### Build/publish the SDK locally
```bash
$ sbt clean publish-local
```
### Running the tests
Before running the tests the first time, you must ensure the npm packages are installed:
```bash
$ npm install
```
Then you can run the tests:
```bash
$ sbt test
```
### Examples
A simple example:
```scala
import io.scalajs.nodejs._
import io.scalajs.npm.mpromise._
val promise = new Promise[(Int, Int)]()
promise.onFulfill { case (a, b) =>
Assert.equal(3, a + b)
}
promise.fulfill((1, 2))
```
Supports asynchronous execution pipelines:
```scala
import io.scalajs.nodejs._
import io.scalajs.npm.mpromise._
val promise = new Promise[Int]()
promise
.`then`(arg => arg + 1)
.`then`(arg => throw new Error(arg + " is an error!"))
.`then`(null, { err =>
Assert.ok(err.isInstanceOf[Error])
Assert.equal("2 is an error", err.message)
})
promise.fulfill(1)
```
Supports conversion to `Future`s.
```scala
import io.scalajs.npm.mpromise._
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
val promise = new Promise[(Int, Int)]()
promise.fulfill((2, 3))
promise.toFuture foreach { case (a, b) =>
println(s"result: ${a + b}") // 5
}
```
Supports promise chaining:
```scala
import io.scalajs.npm.mpromise._
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
def makeMeAPromise(i: Int) = {
val p = new Promise[Int]()
p.fulfill(i)
p
}
val initialPromise = new Promise[Int]()
var returnPromise = initialPromise
for (i <- 0 until 10) {
returnPromise = returnPromise.chain(makeMeAPromise(i))
}
initialPromise.fulfill(1)
returnPromise.toFuture foreach { result =>
println(s"returnPromise: $result") // 9
}
```
### Artifacts and Resolvers
To add the `mpromise` binding to your project, add the following to your build.sbt:
```sbt
libraryDependencies += "io.scalajs.npm" %%% "mpromise" % "0.5.0"
```
Optionally, you may add the Sonatype Repository resolver:
```sbt
resolvers += Resolver.sonatypeRepo("releases")
```