简体   繁体   English

将socket.on作为承诺包装

[英]Wrap socket.on as a promise

I am making a service, with a function init as resolve . 我正在使用函数init作为resolve First, it creates a folder with the id of the connection, then it writes some default files inside. 首先,它创建一个具有连接ID的文件夹,然后在其中写入一些默认文件。 The folder (ie, the id of the connection) should be recorded, which is the purpose of this.dir . 应记录文件夹(即连接的ID),这是this.dir的目的。

The following code calls well initDir . 以下代码调用initDir However, it does not call initFiles (ie, alert("here") is not called). 但是,它不会调用initFiles (即,不调用alert("here") )。 I guess it is because initDir was not made well as a promise. 我想这是因为initDir没有做好承诺。

Does anyone know how to amend it? 有谁知道如何修改它?

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)
        })
}

On the server side, it just sends the connection id after every connection: 在服务器端,它只是在每次连接后发送连接ID:

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

Edit 1: Following the answer of @epiqueras , I modified the code. 编辑1:在@epiqueras的答案之后,我修改了代码。 The following code calls inside init , however it does not call inside initDir .... 以下代码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)
        })
}

Edit 2: after changing initDir.then to this.initDir().then , I have the following code. 编辑2:initDir.then更改为this.initDir().then ,我有以下代码。 It shows well inside init , inside initDir , before initFiles . inside initDir inside initinside initDir inside initinside initDir before initFiles However, it does not show inside initFiles or write files. 但是,它不会显示inside initFiles或write文件中。

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)
    })
};

It's not working because socket.on does not return it's callback's return value. 它不起作用,因为socket.on不返回它的回调的返回值。

Try wrapping the code with a promise and resolve inside the callback instead of just returning. 尝试使用promise包装代码并在回调中解决而不是仅返回。

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

Make sure you are returning the new Promise and that you are resolving the final value you wish to wait for. 确保您返回新的Promise并且您正在解决您希望等待的最终值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM