kimar / Promise.kt

A tiny and simple Kotlin Promise library, written in 26 lines of code ⛷

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Promise.kt

Twitter: @Kidmar GitHub license Build Status Kotlin

tl;dr

A very simple Kotlin Promise library I've written, after coming up with the same library for Swift on my iPad years ago.

Writte in just 26 lines of code.

Usage

Rather simple and more for reference than actually to be used is this very simple implementation of a Promise library.

Succeeding

Promise<String> { resolve, reject ->
	resolve("Bazingaaaa!")
}.then { result ->
	System.out.print(result) // Bazingaaaa!
}

Failing

Promise<String> { resolve, reject -> 
	reject(AnError())
}.fail { error ->
	System.out.print("An error occured $error")
}

Chaining

Promise<String> { resolve, reject -> 
	resolve(1)
}.thenWith { result -> 
	return Promise<Int> { resolve, reject -> 
		resolve(result + 1)
	}
}.thenWith { result -> 
	return Promise<Int> { resolve, reject -> 
		resolve(result + 1)
	}
}.then { result -> 
	System.out.print(result) // 3
}

Contributing

Please take a moment to read and understand the Berlin Code of Conduct.

License

See LICENSE.md

About

A tiny and simple Kotlin Promise library, written in 26 lines of code ⛷

License:Apache License 2.0


Languages

Language:Kotlin 100.0%