pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <legend id='NInmc'><style id='NInmc'><dir id='NInmc'><q id='NInmc'></q></dir></style></legend>
    <tfoot id='NInmc'></tfoot>
      <bdo id='NInmc'></bdo><ul id='NInmc'></ul>

      <i id='NInmc'><tr id='NInmc'><dt id='NInmc'><q id='NInmc'><span id='NInmc'><b id='NInmc'><form id='NInmc'><ins id='NInmc'></ins><ul id='NInmc'></ul><sub id='NInmc'></sub></form><legend id='NInmc'></legend><bdo id='NInmc'><pre id='NInmc'><center id='NInmc'></center></pre></bdo></b><th id='NInmc'></th></span></q></dt></tr></i><div class="aa27o1n" id='NInmc'><tfoot id='NInmc'></tfoot><dl id='NInmc'><fieldset id='NInmc'></fieldset></dl></div>

      <small id='NInmc'></small><noframes id='NInmc'>

    1. 在完成函數(shù)本身的所有操作之前,Node 是否可以從

      Could Node feasibly return a value from a function call before completing all operations within the function itself?(在完成函數(shù)本身的所有操作之前,Node 是否可以從函數(shù)調(diào)用中返回一個值?) - IT屋-程序員軟件開發(fā)
    2. <legend id='1Ypsr'><style id='1Ypsr'><dir id='1Ypsr'><q id='1Ypsr'></q></dir></style></legend>
      • <i id='1Ypsr'><tr id='1Ypsr'><dt id='1Ypsr'><q id='1Ypsr'><span id='1Ypsr'><b id='1Ypsr'><form id='1Ypsr'><ins id='1Ypsr'></ins><ul id='1Ypsr'></ul><sub id='1Ypsr'></sub></form><legend id='1Ypsr'></legend><bdo id='1Ypsr'><pre id='1Ypsr'><center id='1Ypsr'></center></pre></bdo></b><th id='1Ypsr'></th></span></q></dt></tr></i><div class="vv31f38" id='1Ypsr'><tfoot id='1Ypsr'></tfoot><dl id='1Ypsr'><fieldset id='1Ypsr'></fieldset></dl></div>

          <tfoot id='1Ypsr'></tfoot>

          <small id='1Ypsr'></small><noframes id='1Ypsr'>

            <bdo id='1Ypsr'></bdo><ul id='1Ypsr'></ul>
              <tbody id='1Ypsr'></tbody>

              • 本文介紹了在完成函數(shù)本身的所有操作之前,Node 是否可以從函數(shù)調(diào)用中返回一個值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                限時送ChatGPT賬號..

                我無法理解 Node 在并行處理和函數(shù)調(diào)用返回值方面的操作方式.

                I'm having trouble understanding how Node operates regarding it's parallel processing and returning values from function calls.

                僅供參考:下面的 gulp 函數(shù)僅作為此問題的示例而創(chuàng)建.

                FYI: The gulp function below is merely created as an example for this question.

                Read a large file語句完成處理(大文件已從文件系統(tǒng)完全讀取并添加流)之前,該函數(shù)是否可能返回流,還是 Node 足夠聰明,可以在返回之前完成所有語句?

                Is it possible that the function could return the stream before the Read a large file statement has finished processing (the large file has been fully read from the file system and the stream has been added), or is Node smart enough to complete all statements before returning?

                function moveFiles(){
                
                    var gulp = require('gulp'),
                        stream = require('merge-stream')();
                
                    // Read a large file
                    stream.add(gulp.src('src/large-file.txt')
                        .pipe(gulp.dest('dest/'))
                    );
                
                    // Read a small file
                    stream.add(gulp.src('src/small-file.txt')
                        .pipe(gulp.dest('dest/'))
                    );
                
                    return (stream.isEmpty() ? null : stream);
                
                }
                

                推薦答案

                在完成函數(shù)本身的所有操作之前,Node 是否可以從函數(shù)調(diào)用中返回一個值?

                Could Node feasibly return a value from a function call before completing all operations within the function itself?

                這是一個棘手的問題.答案是否定的,在某種程度上,返回一個值意味著函數(shù)已完成執(zhí)行,它從堆棧中取回并且它永遠不會再做任何事情 - 當然除非它在另一個時間被調(diào)用,但關(guān)鍵是這個特定的調(diào)用結(jié)束了.

                This is a tricky question. The answer is no, in a way that returning a value means that the function is finished executing, it's taken back from the stack and it will never do anything again - unless it's invoked another time of course, but the point is that this particular invocation is over.

                但棘手的部分是 函數(shù) 已完成執(zhí)行,這并不意味著它不能安排將來??發(fā)生其他事情.稍后會變得更復(fù)雜,但首先是一個非常簡單的示例.

                But the tricky part is that it's the function that's finished executing and it doesn't mean that it couldn't schedule something else to happen in the future. It will get more complicated in a minute but first a very simple example.

                function x() {
                    setTimeout(function () {
                        console.log('x1'));
                    }, 2000);
                    console.log('x2');
                    return;
                    console.log('x3');
                }
                

                這里當你調(diào)用 x() 然后它會安排 another 函數(shù)在 2 秒后運行,然后它會打印 x2 然后它將返回 - 此時此函數(shù)無法再為該調(diào)用執(zhí)行任何其他操作.

                Here when you call x() then it will schedule another function to run after 2 seconds, then it will print x2 and then it will return - at which point this function cannot do anything else ever again for that invocation.

                這意味著 x3 永遠不會被打印,但 x1 最終會被打印 - 因為它是另一個在超時觸發(fā)時將被調(diào)用的函數(shù).匿名函數(shù)將被調(diào)用不是因為 x() 函數(shù)在返回后可以做任何事情,而是因為它設(shè)法在返回之前安排了超時時間.

                It means that x3 will never get printed, but x1 will eventually get printed - because it's another function that will be called when the timeout fires. The anonymous function will get called not because the x() function can do anything after it returns, but because it managed to schedule the timeout before it returned.

                現(xiàn)在,一個函數(shù)可以返回一個承諾,該承諾將在一段時間后得到解決,而不是僅僅安排未來發(fā)生的事情.例如:

                Now, instead of just scheduling things to happen in the future, a function can return a promise that will get resolved some time later. For example:

                function y() {
                    console.log('y1');
                    return new Promise(function (resolve, reject) {
                        setTimeout(function () {
                            resolve('message from y()');
                        }, 2000);
                    });
                    console.log('y2');
                }
                

                現(xiàn)在,當你運行時:

                var promise = y();
                

                將會發(fā)生的是 y1 將被打印,一個新的承諾將被返回并且 y2 將永遠不會被打印,因為此時 y() 返回并且不能做任何其他事情.但它設(shè)法安排了一個超時,將在兩秒后解決這個承諾.

                what will happen is that y1 will get printed, a new promise will get returned and y2 will never get printed because at that point y() returned and cannot do anything else. But it managed to schedule a timeout that will resolve the promise after two seconds.

                您可以通過以下方式觀察它:

                You can observe it with:

                promise.then(function (value) {
                    console.log(value);
                });
                

                所以通過這個例子你可以看到,雖然 y() 函數(shù)本身返回并且不能做任何其他事情,但將來可以調(diào)用其他一些(在這種情況下是匿名的)函數(shù)并完成y() 函數(shù)已啟動的作業(yè).

                So with this example you can see that while the y() function itself returned and cannot do anything else, some other (anonymous in this case) function can be called in the future and finish the job that the y() function has initiated.

                所以我希望現(xiàn)在清楚為什么這是一個棘手的問題.在某種程度上,一個函數(shù)在返回后不能做任何事情.但它可以安排一些其他函數(shù)作為超時、事件處理程序等,這些函數(shù)可以在函數(shù)返回后執(zhí)行某些操作.如果函數(shù)返回的東西是一個承諾,那么調(diào)用者可以很容易地在它準備好時觀察到未來的值.

                So I hope now it's clear why it's a tricky question. In a way a function cannot do anything after returning. But it could have scheduled some other functions as timeouts, event handlers etc. that can do something after the functions returns. And if the thing that the function returns is a promise then the caller can easily observe the value in the future when it's ready.

                所有示例都可以通過使用箭頭函數(shù)來簡化,但我想明確指出這些都是單獨的函數(shù),其中一些是命名的,一些是匿名的.

                All of the examples could be simplified by using the arrow functions but I wanted to make it explicit that those are all separate functions, some of them are named, some are anonymous.

                有關(guān)更多詳細信息,請參閱其中一些答案:

                For more details see some of those answers:

                • 詳細說明如何使用回調(diào)和承諾
                • 解釋如何在復(fù)雜的請求處理程序
                • 在AJAX 請求示例
                • 對回調(diào)、承諾和如何做的解釋訪問異步返回的數(shù)據(jù)

                這篇關(guān)于在完成函數(shù)本身的所有操作之前,Node 是否可以從函數(shù)調(diào)用中返回一個值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                Browserify, Babel 6, Gulp - Unexpected token on spread operator(Browserify,Babel 6,Gulp - 傳播運算符上的意外令牌)
                Is it possible to pass a flag to Gulp to have it run tasks in different ways?(是否可以將標志傳遞給 Gulp 以使其以不同的方式運行任務(wù)?)
                Why do we need to install gulp globally and locally?(為什么我們需要在全局和本地安裝 gulp?)
                How to run Gulp tasks sequentially one after the other(如何一個接一個地依次運行 Gulp 任務(wù))
                Stylesheet not loaded because of MIME-type(由于 MIME 類型而未加載樣式表)
                Visual Studio 2015 crashes when opening Javascript files(打開 Javascript 文件時 Visual Studio 2015 崩潰)

                <tfoot id='xBWps'></tfoot>

                  <bdo id='xBWps'></bdo><ul id='xBWps'></ul>

                • <i id='xBWps'><tr id='xBWps'><dt id='xBWps'><q id='xBWps'><span id='xBWps'><b id='xBWps'><form id='xBWps'><ins id='xBWps'></ins><ul id='xBWps'></ul><sub id='xBWps'></sub></form><legend id='xBWps'></legend><bdo id='xBWps'><pre id='xBWps'><center id='xBWps'></center></pre></bdo></b><th id='xBWps'></th></span></q></dt></tr></i><div class="xewgulu" id='xBWps'><tfoot id='xBWps'></tfoot><dl id='xBWps'><fieldset id='xBWps'></fieldset></dl></div>
                    <legend id='xBWps'><style id='xBWps'><dir id='xBWps'><q id='xBWps'></q></dir></style></legend>

                          <tbody id='xBWps'></tbody>

                          <small id='xBWps'></small><noframes id='xBWps'>

                        • 主站蜘蛛池模板: 称重传感器,测力传感器,拉压力传感器,压力变送器,扭矩传感器,南京凯基特电气有限公司 | 发电机价格|发电机组价格|柴油发电机价格|柴油发电机组价格网 | AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 生物风-销售载体,基因,质粒,ATCC细胞,ATCC菌株等,欢迎购买-百风生物 | 电子海图系统-电梯检验系统-智慧供热系统开发-商品房预售资金监管系统 | 健康管理师报名入口,2025年健康管理师考试时间信息网-网站首页 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 贴片电容-贴片电阻-二三极管-国巨|三星|风华贴片电容代理商-深圳伟哲电子 | 冷水机,风冷冷水机,水冷冷水机,螺杆冷水机专业制造商-上海祝松机械有限公司 | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 | 圆盘鞋底注塑机_连帮鞋底成型注塑机-温州天钢机械有限公司 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 楼承板-开口楼承板-闭口楼承板-无锡海逵 | 地脚螺栓_材质_标准-永年县德联地脚螺栓厂家 | 福州时代广告制作装饰有限公司-福州广告公司广告牌制作,福州展厅文化墙广告设计, | 环保袋,无纺布袋,无纺布打孔袋,保温袋,环保袋定制,环保袋厂家,环雅包装-十七年环保袋定制厂家 | 谷歌关键词优化-外贸网站优化-Google SEO小语种推广-思亿欧外贸快车 | 昊宇水工|河北昊宇水工机械工程有限公司| 清水-铝合金-建筑模板厂家-木模板价格-铝模板生产「五棵松」品牌 | U拓留学雅思一站式服务中心_留学申请_雅思托福培训 | 精密机械零件加工_CNC加工_精密加工_数控车床加工_精密机械加工_机械零部件加工厂 | 天津散热器_天津暖气片_天津安尼威尔散热器制造有限公司 | 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | 三防漆–水性三防漆–水性浸渍漆–贝塔三防漆厂家 | 气动|电动调节阀|球阀|蝶阀-自力式调节阀-上海渠工阀门管道工程有限公司 | 广州物流公司_广州货运公司_广州回程车运输 - 万信物流 | 空心明胶胶囊|植物胶囊|清真胶囊|浙江绿键胶囊有限公司欢迎您! | 河南中专学校|职高|技校招生-河南中职中专网 | 钛合金标准件-钛合金螺丝-钛管件-钛合金棒-钛合金板-钛合金锻件-宝鸡远航钛业有限公司 | 全自动包装机_灌装机生产厂家-迈驰包装设备有限公司 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | 不锈钢钢格栅板_热浸锌钢格板_镀锌钢格栅板_钢格栅盖板-格美瑞 | 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | 智能化的检漏仪_气密性测试仪_流量测试仪_流阻阻力测试仪_呼吸管快速检漏仪_连接器防水测试仪_车载镜头测试仪_奥图自动化科技 | 拉力机-万能试验机-材料拉伸试验机-电子拉力机-拉力试验机厂家-冲击试验机-苏州皖仪实验仪器有限公司 | 玉米深加工机械,玉米加工设备,玉米加工机械等玉米深加工设备制造商-河南成立粮油机械有限公司 | 杭州中策电线|中策电缆|中策电线|杭州中策电缆|杭州中策电缆永通集团有限公司 | 食药成分检测_调料配方还原_洗涤剂化学成分分析_饲料_百检信息科技有限公司 | 中开泵,中开泵厂家,双吸中开泵-山东博二泵业有限公司 | 苏州防水公司_厂房屋面外墙防水_地下室卫生间防水堵漏-苏州伊诺尔防水工程有限公司 | 广州云仓代发-昊哥云仓专业电商仓储托管外包代发货服务 |