loretoparisi / fasttext.js

FastText for Node.js
MIT License
192 stars 28 forks source link

async/await version #17

Closed bittlingmayer closed 2 years ago

bittlingmayer commented 5 years ago

This would be easy to do with promisify in newer versions of Node.

const {promisify} = require('util');
exports.exists = promisify(train);
...

Personally I far prefer async/await to the callback syntax.

loretoparisi commented 5 years ago

@bittlingmayer I will check this out, VSCode has a new functionality that let you convert Promise.then().catch() in the async await pattern.

loretoparisi commented 5 years ago

@bittlingmayer I have just realized that Node10 has worker threads!!!! that is a huge improvements, and will speed up fastText execution a lot!!! Check this out!

https://github.com/a1xon/Node-Workers-Example/blob/master/worker.js

loretoparisi commented 2 years ago

This code snippet show how to use async/await

    let FastText = require('../lib/index');
    let ft = new FastText({
        predict: {
            // k most likely labels for each line <= labels count
            mostlikely: 2,
            verbosity: 2,
            // true to normalize input text before prediction
            normalize: false
        },
        loadModel: MODELS_ROOT + '/lid.176.ftz' // must specifiy filename and ext
    });
    let text = "Bonjour à tous. Ceci est du français";
    // use WASM api
    try {
        await ft.loadWASM();
        var labels = await ft.predictWASM(text);
        console.log("TEXT:", text, "\nPREDICT:", labels);
    } catch (error) {
        console.error("predict error", error);
    }