Socket
Book a DemoInstallSign in
Socket

remote-promises

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

remote-promises

Create native ES6 promises and resolve them externally

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
758
-24.88%
Maintainers
1
Weekly downloads
 
Created
Source

🕹️🤞

remote-promises

Create native ES6 promises and resolve them externally.

NPM Version Shield NPM Size Shield

Get Started

Ensure its installed as a dependency from your console:

npm i remote-promises

Then import the RemotePromise class before attempting the following examples

import { RemotePromise } from 'remote-promises'

Remotely Controlling the Promise

I guess this is why you're here – use .resolve() or .reject() to satisfy the promise.

When you do this, the status property changes from "pending" to either "resolved" or "rejected".

// Instantiate a new remote promise
const rp = new RemotePromise()

rp.resolve('some resolved value')
//- rp.reject(new Error("500: The whole system's blown!"))

rp.status // 'resolved'

Awaiting a Remote Promise

RemotePromise can be used with the await keyword just like the native promises.

const rp = new RemotePromise()
rp.resolve('any value')

// Use await in an asynchronous context
(async () => {
  console.log(await rp) // 'any value'
})()

But when await is used with a rejected promise, the rejection is thrown like an error and can be caught.

const rp = new RemotePromise()

// Gotta catch 'em all!
(async () => {
  try {
    await rp
  } catch (reason) {
    console.log(reason) // 'pocket monsters'
  }
})()

rp.reject('pocket monsters')

Using Then and Catch

If you're not a fan of the async/await style, then you can use then/catch as you normally would with promises. These can be chained too.

const rp = new RemotePromise()

// then/catch can be called the promise is satisfied
rp.then(value => console.log('success', value))
rp.catch(reason => console.log('flop', reason)) // 'flop', 'fish'

// Satisfy the promise
rp.reject('fish')

// Cheeky little example involving chains
rp
  .catch(() => 'flip')
  .then(value => console.log(value)) // 'flip'

Involving Types for TypeScript

As with the native promises, you're able to define an interface for a fulfilled promise value.

// Define your structure
interface SomeStructure {
  status: 200
  result: string
}

// Create a new typed remote promise
const rp = new RemotePromise<SomeStructure>()

// Must resolve with an object according to the type
rp.resolve({
  status: 200,
  result: 'samosas'
})

// Start writing a callback...
rp.then(val => {
  // val is of type SomeStructure
})

FAQs

Package last updated on 18 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.