传递(object.function)作为参数

我正在使用Promises构建一个简单的拖放上传器,但是我收到了Uncaught SyntaxError: Unexpected token . 当我尝试传递一个方法(object.function)作为承诺的回调函数的参数解析/拒绝。

我期待我的组织承诺回调为下面的方法resolvereject的对象,以便在上传的情况下,这将是resolve.uploadreject.upload ,例如,如果我有发言权的用户身份验证的登入承诺,然后它会即, resolve.signinreject.signin

当前的承诺模式是链接then(),catch(),reject(),resolve()这是一个可怕的编程模式,如果你使用套接字和链2+异步调用代码变得很难管理,因为你越来越多地嵌套匿名函数也被称为厄运模式的金字塔,为了避免这种模式,我使用了另一个我用过无数次的异步调用,因为我在下面的代码中设置异步事件侦听器client.addEventListener(prop, callback.upload[prop]); 并在一个单独的对象callback.upload.event处理它们。

以同样的方式,我正在处理异步请求的回调响应,我试图实现承诺以避免在如此长的无法管理的函数中链接/嵌套5-10异步请求,但是为回调函数传递方法(object.function)对于promise function(resolve.upload,reject.upload)似乎不起作用,但是我传递了一个全局函数(函数),即resolvereject它将工作得很好,但是这不好,因为它会是混淆全局命名空间,这就是为什么我想传递一个object.method

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="styles/upload.css"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
    <script>
        //EVENT LISTENERS
        var listen = {
            drag: ['dragenter','dragover','drop','dragleave'],
            async: ['readystatechange','loadstart','progress','abort','error','load','timeout','loadend']
        };
        //PROMISES
       var executor = {
            //THIS IS WHERE MY PROBLEM LIES
            upload: function(resolve.upload,reject.upload){
                var data = new FormData();
                var client = new XMLHttpRequest();
                data.append('xls', executor.upload.files); 
                for(prop of listen.async){ 
                    client.addEventListener(prop, callback.upload[prop]);
                }
                client.open("POST", "/core/upload.php");
                client.send(data);
            }
        };
        //PROMISES HANDLERS
        var resolve = {
            upload: function(value){
                 console.log(value);
            }
        };
        var reject = {
             upload: function(reason){
                 console.log(reason);
             }
        };
        //USER HANDLERS
        var handles = {
            upload: {
                dragenter: function(e){
                    e.target.classList.remove('emboss');
                }
                dragover: function(e){
                     e.preventDefault();
                }
                drop: function(e){
                    e.preventDefault();
                    executor.upload.files = e.dataTransfer.files[0];
                    //CREATE PROMISE
                    var p = new Promise(executor.upload);
                    console.log(p);
                    //CHECK PROMISE STATUS, EVERY 3 SECS
                    setInterval(function(){
                        console.log(p);
                    }, 3000);
                }
                dragleave: function(e){
                     e.target.classList.add('emboss');
                }
            }
        };

        //ASYNC HANDLERS
        var callback = {
            upload: {
                readystatechange: function(e){
                    console.log(e.target.readyState);
                }
                loadstart: function(e){ 
                    console.log('loadstart');
                }
                progress: function(e){
                    console.log('progress');
                }
                abort: function(e){
                    console.log('abort');
                }
                error: function(e){
                    console.log('error');
                }
                load: function(e){
                    console.log('load');
                }
                timeout: function(e){
                    console.log('timeout');
                }
                loadend: function(e){
                    console.log('loadend');
                }
            }   
        };

        //INITIALIZATION
        function init(){
            var dropbox = document.getElementById('dropbox');
            for(prop of listen.drag){ 
                dropbox.addEventListener(prop, handles.upload[prop]);}
            };
        //CALL INIT
        document.addEventListener('DOMContentLoaded', init);
    </script>
    </head>
    <body>
        <div id="dropbox" class="fa fa-file-excel-o fa-4x emboss"></div>
    </body>
</html>

您不能将自己的回调传递给传递给Promise构造函数的函数。 你可以做的是在后续链中注册回调,例如:

var p = new Promise(executor.upload).then(resolve.upload, reject.upload);

但是,为了实现这一点, execute.upload函数必须根据XMLHttpRequest对象的实际情况实际调用传递的resolvereject参数:

executor.upload = (resolve, reject) => {
    var client = new XMLHttpRequest();

    client.addEventListener('readystatechange', (e) => {
        // call `resolve` or `reject` as appropriate
    });

    // then register any more specific handlers
    for (prop of listen.async){ 
        client.addEventListener(prop, callback.upload[prop]);
    }

    client.open("POST", "/core/upload.php");
    client.send(data);
}

这是在Promise构造函数的回调中调用resolve()reject()的动作,最终导致处理程序注册.then被调用。


在“fatarrow”语法中:

(param1, param2) => { thing to be returned }

在左边的parens中的名字是{ thing to be returned }部分的本地名称。 事实上,除了“this”的差异之外,关于所有的fatarrow等同于语法上的简写。 所以这两个函数定义基本相同:

blah: ( alpha, bravo ) => alpha + ", " + bravo
function blah ( alpha, bravo ) { return alpha + ", " + bravo; }

你要:

upload: ( resfunc, rejfunc ) => { ...etc... }

无论你在哪里上传CALL,你都会去哪里:

upload(resolve.upload, reject.upload)

我也不确定你对承诺的使用。 这是我的理解,回调给你给你打电话。 就是这样。

var promise = new Promise(
    function(resolve, reject) {
        img.onload = function() {
            ctx.drawImage(img, 300, 100);
            resolve();                          // done, tell the promise
        };
        img.src = 'data:image/gif;base64,R0lGODlhCwALAIAAAAAA3pn/ZiH5BAEAAAEALAAAAAALAAsAAAIUhA+hkcuO4lmNVindo7qyrIXiGBYAOw==';
    }
);

然后你可以使用像这样的东西:

promise.then ( function () { /* promise says it is done, do something */ } );
链接地址: http://www.djcxy.com/p/55501.html

上一篇: Passing (object.function) as a parameter

下一篇: Basic Javascript promise implementation attempt