New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

f-async-resolver

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

f-async-resolver

Make your functions async without the need of doing it in the same Function

latest
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

f-async-resolver

Make your functionas async.

This module is made so you can make your funcions async and return a value without the need of having it in the same function. For example returning an answer from a server in a WebSocket connection.

Installing

npm i f-async-resolver

Initializing

const async = require('f-async-resolver').resolver;
const resolver = new async();

Examples

Awaiting for a Number

const async = require('f-async-resolver');
const resolver = new async.resolver();
function number() {
    return new Promise(solver => {
        resolver.await('number', solver);
    });
}

number.then(number => {
    console.log(number);
});

setTimeout(() => {
    resolver.resolve('number', 15);
}, 1000);

/*
Output (1 Second after running the Code) 
> 15
*/

Awaiting for WebSockets message.

const async = require('f-async-resolver');
const resolver = new async.resolver();
const WebSocket = require('ws');


class database {
    constructor(IP) {
        this.ws = new WebSocket('ws://127.0.0.1:4000');  // Connects to a Server.
        this.ws.onopen = this.onopen.bind(this);         // Simple function Bind.
        this.ws.onmessage = this.onmessage.bind(this);   // Simple function Bind.
    }
    onopen() {
        console.log('Connected!');
    }
    get() {
        return new Promise(solver => {                  // Returns a Promise and makes 'solver' to solve.
            this.ws.send({
                type: 'getDatabase'
            });                                          // Sends a message to the Server
            resolver.await('getDB', solver);             // Sets label and function to Solve.
        });
    }
    onmessage(msg) {
        resolver.resolve('getDB', msg);                  // Solves 'getDB' label with msg value.
    }
};

const connection = new database('ws://127.0.0.1:4000');
connection.get().then(msg => {
    console.log('Awaited for ' + msg);                   // Shows the message Recieved.
});

Keywords

resolver

FAQs

Package last updated on 31 May 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