1.0.6 • Published 4 years ago

fetch-dummy-data v1.0.6

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

fetch-dummy-data

Fetching dummy data asynchronously


A dummy data fetching module using javascript callback, promise and async/await.

fetch-dummy-data allows you to check and understand the working of javascipt's asynchronous function calls by using three different ways, namely: 1. JavaScript Callbacks 2. JavaScript Promises 3. JavaScript Async

About Callbacks, Promises & Async/Await

Callbacks

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. Note, however, that callbacks are often used to continue code execution after an asynchronous operation has completed — these are called asynchronous callbacks. A good example is the callback functions executed inside a .then() block chained onto the end of a promise after that promise fulfills or rejects. This structure is used in many modern web APIs, such as fetch().

Promises

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

Async/Await

Async functions are instances of the AsyncFunction constructor, and the await keyword is permitted within them. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.

Async functions can contain zero or more await expressions. Await expressions suspend progress through an async function, yielding control and subsequently resuming progress only when an awaited promise-based asynchronous operation is either fulfilled or rejected. The resolved value of the promise is treated as the return value of the await expression. Use of async / await enables the use of ordinary try / catch blocks around asynchronous code.

Features

  • .callbackProto to fetch data asynchronously using javascript's callback feature by including userId as the function parameter. The job of this function is to fetch the name of the Presidential candidate of 2020 US Presidential ELections according to the userId passed as paramater to the function.

  • .promiseProto to fetch data asynchronously using javascript's promise feature by including userId as the function parameter. The job of this function is to fetch the name of the Presidential candidate of 2020 US Presidential ELections according to the userId passed as paramater to the function.

  • .asyncAwaitProto to fetch data asynchronously using javascript's async/await features by including userId as the function parameter. The job of this function is to fetch the name of the Presidential candidate of 2020 US Presidential ELections according to the userId passed as paramater to the function.


  • Demo

  • Installation

  • Importing

  • Fetching Dummy Data

    • API

    • Usage

  • Linting (ESLint)

Demo

Running callback

.callbackProto

Running promise

.promiseProto

Running async/await

.asyncAwaitProto

Installation

npm i --save-dev install fetch-dummy-data@1.0.6

yarn add -D install fetch-dummy-data@1.0.6

Importing

fetch-dummy-data is a default export so it can be imported with whatever name you like.

const dummy = require('fetch-dummy-data@1.0.6');

Fetching Dummy Data

API

callbackProto(userId)

.callbackProto:
  • parameters: Number the userId of the data to be fetched.

promiseProto(userId)

.promiseProto:
  • parameters: Number the userId of the data to be fetched.

asyncAwaitProto(userId)

.asyncAwaitProto:
  • parameters: Number the userId of the data to be fetched.

Usage

.callbackProto(userId)

  1. dummy.callbackProto(2);

    returns String "Fetched Data: Donald Trump"

  2. dummy.callbackProto(-2);

    returns String "Error: Invalid ID"

  3. dummy.callbackProto();

    returns String "Error: ID missing"

.promiseProto(userId)

  1. dummy.promiseProto(1);

    returns String "Fetched Data: Joe Biden"

  2. dummy.promiseProto(-2);

    returns String "Error: Invalid ID"

  3. dummy.promiseProto();

    returns String "Error: ID missing"

.asyncAwaitProto(userId)

  1. dummy.asyncAwaitProto(4);

    returns String "Fetched Data: Howie Hawkins"

  2. dummy.asyncAwaitProto(-2);

    returns String "Error: Invalid ID"

  3. dummy.asyncAwaitProto();

    returns String "Error: ID missing"

Linting (ESLint)

1. ESLint installation

  • npm install -g eslint-cli - To install eslint command line interface
  • npm install eslint - To install eslint

2. Running Lint

  • eslint ./src/** - To lint only the source code contents and ignore any other file in the package, including docs, coverage, etc.

License

ISC

1.0.6

4 years ago

1.0.5

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.4

4 years ago