包装 socket.on 作为承诺

Wrap socket.on as a promise

我正在制作一项服务,其功能 initresolve。首先,它创建一个包含连接 ID 的文件夹,然后在其中写入一些默认文件。应该记录文件夹(即连接的id),这就是this.dir.

的目的

下面的代码调用得很好initDir。但是,它不会调用 initFiles(即不会调用 alert("here"))。我猜是因为initDir没有兑现承诺。

有谁知道怎么修改吗?

this.files = [ ... ... ];

this.dir = "public/tmp/default/";

this.initDir = function () {
    var socket = io.connect();
    return socket.on('id', function (id) {
        var dir = "public/tmp/" + id + "/";
        return $http.post('mkdir', { dir: dir })
            .then(function () {
                this.dir = dir;
            })
    })
} 

this.initFiles = function (dir, files) {
    return $http.post('writeFile', { dir: dir, file: files[0] })
        .then(function (res) {
            $http.post('writeFile', { dir: dir, file: files[1] })
                .then(function (res) {
                    console.log("saved 2 files")
                })
            })
    })
}

this.init = function () {
    return this.initDir() 
        .then(function (res) {
            alert("here");
            this.initFiles(this.dir, this.files)
        })
}

在服务器端,它只是在每次连接后发送连接ID:

io.sockets.on('connection', function (socket) {
    socket.emit('id', socket.id);
}

编辑1:根据@epiqueras的回答,我修改了代码。下面的代码调用 inside init,但它不调用 inside initDir...

var dirP = "public/tmp/default/";

this.initDir = function () {
    return new Promise(function (resolve, reject) {
        alert("inside initDir")
        var socket = io.connect();
        socket.on('id', function (id) {
            var dir = "public/tmp/" + id + "/";
            $http.post('mkdir', { dir: dir })
                .then(function () {
                    dirP = dir;
                    resolve(dirP)
                })
        })
    })      
} 

this.init = function (files) {
    alert("inside init");
    return initDir
        .then(function (dir) {
            alert("before initFiles");
            this.initFiles(dir, files)
        })
}

编辑 2:initDir.then 更改为 this.initDir().then 后,我有以下代码。它显示很好inside initinside initDirbefore initFiles。但是,它不显示 inside initFiles 或写入文件。

this.initDir = function () {
    return new Promise(function (resolve, reject) {
        console.log("inside initDir")
        var socket = io.connect();
        socket.on('id', function (id) {
            var dir = "public/tmp/" + id + "/";
            $http.post('mkdir', { dir: dir })
                .then(function () {
                    dirP = dir;
                    resolve(dirP)
                })
        })
    })      
}; 

this.initFiles = function (dir, files) {
    console.log("initFiles: " + dir);
    return $http.post('writeFile', { dir: dir, file: files[0] })
        .then(function (res) {
            $http.post('writeFile', { dir: dir, file: files[1] })
                .then(function (res) {
                    console.log("saved 2 files")
                })
            })
    })
}

this.init = function (files) {
    console.log("inside init");
    return this.initDir().then(function (dir) {
        console.log("before initFiles " + dir + JSON.stringify(files));
        this.initFiles(dir, files)
    })
};

它不起作用,因为 socket.on 不是 return 它是回调的 return 值。

尝试用承诺包装代码并在回调中解析而不是 returning。

initDir() {
    return new Promise(function(resolve, reject) {
        socket.on.....
            $http.post.....then(resolve)
    }
}

确保您正在 returning 新的 Promise 并且正在解析您希望等待的最终值。