p-ready
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

p-ready

Ready state promise wrapper

Api

let PReady = require('p-ready')

let ready = new PReady
ready.then(value => console.log('Done', value)) // Just like promise
ready.resolve(1)

// Ok, chaining is also supported
.reset() // It's about creating a new promise, try to comment this line
.then(value => console.log('Done', value))
.resolve(2)
.then(value => console.log('Done', value))
// See more in index.d.ts

Usage example

let PReady = require('p-ready')

class Transport {
	ready = new PReady

	connect() {
		// Connection simulation
		console.log('Connecting')

		setTimeout(() => {
			this.ready.resolve() // Connected
		}, 3000)
	}

	reconnect() {
		this.ready.reset()
		this.connect()
	}

	async send(data) {
		await this.ready // Here it is
		// or use this.ready.promise for direct access
		console.log('The', data, 'was sent, hooray!')
	}
}



let transport = new Transport
transport.connect()

let num = 0
setInterval(() => {
	// We send data regardless of the connection
	transport.send('test ' + num)
	num++
}, 1000)

setTimeout(() => {
	transport.reconnect() // Suppose we reconnect
}, 3000)

Package Sidebar

Install

npm i p-ready

Weekly Downloads

1

Version

2.0.1

License

MIT

Unpacked Size

3.77 kB

Total Files

7

Last publish

Collaborators

  • mytecor