Giter VIP home page Giter VIP logo

Comments (4)

azu avatar azu commented on May 18, 2024


内部でpromise実行させる場合も次のメソッドに前回の結果を渡せるようにし

from promises-book.

azu avatar azu commented on May 18, 2024

fs をラップした例を書くのが良さそう。

  • file read
  • fileの変更処理
  • file write

とういう事をpromise chainで書けるようなラッパ。

errorはcatchできるようにすると良さそう。
変更処理はユーザー定義関数を渡す感じで、
引数にはファイルのpromise オブジェクトがくる.
Promise.resolveで受けるケースにもなる

もっと高速にするならstreamを使うべきという展開と、
bluebirdとかのpromisefyについて触れる

from promises-book.

azu avatar azu commented on May 18, 2024

シンプルに書くとこんな感じになりそうだけど、
promiseをラップした関数でcatchはどういう扱いにするのがいいんだろ?
普通にcatchとして外にだすのがいいのかな

"use strict";
var fs = require("fs");
function File() {
    this.promise = Promise.resolve();
}
File.prototype.runPromise = function (fn) {
    var that = this;
    this.promise = this.promise.then(function (value) {
        return fn.call(that, value)
    });
};
File.prototype.read = function (filePath) {
    this.runPromise(function () {
        return fs.readFileSync(filePath, "utf-8");
    });
    return this;
};
File.prototype.transform = function (fn) {
    this.runPromise(fn);
    return this;
};
File.prototype.write = function (filePath) {
    this.runPromise(function (data) {
        return fs.writeFileSync(filePath, data)
    });
    return this;
};
File.prototype["catch"] = function (onRejected) {
    return this.promise.catch(onRejected);

};
module.exports.File = File;
module.exports.readAsPromise = function (filePath) {
    var file = new File();
    return file.read(filePath);
};

from promises-book.

azu avatar azu commented on May 18, 2024

http://azu.github.io/promises-book/#_promise_6 追加した

from promises-book.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.