js-deferrable

go-like defer implementation for javascript

Usage no npm install needed!

<script type="module">
  import jsDeferrable from 'https://cdn.skypack.dev/js-deferrable';
</script>

README

deferrable

Build Status

A npm module with go-like defer implementation

Installation

npm install js-deferrable --save
yarn add js-deferrable
bower install js-deferrable --save

Usage

Javascript

const deferable = require('js-deferrable');
const fs = require('fs');

const someFunction = deferrable(async (defer, arg1, arg2) => {
    const desc = await fs.open('someFile.txt');
    defer(()=>fs.close(desc));
    // ...
    // some actions with file, fs.close will be called automaticalle
});

const someSyncFunction = deferrable((defer, arg1, arg2) => {
        const desc = fs.openSync('someFile.txt');
        defer(()=>fs.closeSync(desc));
        // ...
        // some actions with file, fs.closeSync will be called automaticalle
}, true)

TypeScript

import deferrable from 'js-deferrable';
import * as fs from require('fs');

const someFunction = deferrable(async (defer, arg1, arg2) => {
    const desc: number = await fs.open('someFile.txt');
    defer(()=>fs.close(desc));
    // ...
    // some actions with file, fs.close will be called automaticalle
});

AMD

define(function(require,exports,module){
  var deferrable = require('js-deferrable');
});

Test

npm run test