javascript - 如何编写 JS 函数来执行某些操作,然后在该过程完成后返回?

标签 javascript function

我正在向现有函数添加一些功能。我需要在当前例程的中间插入一个额外的步骤。我知道如何转到第二个函数,但我不知道第二个例程完成后如何返回主函数。

function step1(){
    perform ajax call to see if student is assigned to a project
    step1_subfunction()
    //  wait here until step1_subfunction is done
    do some more stuff with response from user
}

function step1_subfunction(){
    prompt user via jQuery dialog, 'Add or move employee to the project?'
    //  return to step1 with answer returned from user and resume
}

我会用谷歌搜索这个,但我不知道这个“进程”是否有名称。在我使用 COBOL 的时代,我们称之为 gosub。

更新: 第 1 步 执行 ajax 调用以查看员工是否已分配到项目。如果response.status = 'Assigned',将通过 jQuery 对话框询问用户“是否要将员工复制或移动到此项目?”。 jQuery 对话框将是step1_subroutine。答案将被传回step1函数。步骤 1 的剩余部分只是将一个值放置在“复制”或“移动”的隐藏文本字段中。

最佳答案

您所拥有的将执行您所描述的操作,但可能无法使用户的数据可用于函数step1(),而无需在函数step1_subfunction()中返回。下面我修改了您的示例代码来演示值的传递。

function step1(){
   //do some stuff
   var returnValFromFunction = step1_subfunction();
   //  wait here until step1_subfunction is done
   //  Now use returnValFromFunction, it contains the information from the user
   do some more stuff with response from user
}

function step1_subfunction(){
    prompt user for some information
   //  return to step1 with information returned from user and resume
   return userResponse;
}

关于javascript - 如何编写 JS 函数来执行某些操作,然后在该过程完成后返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503442/

相关文章:

javascript - 根据您的经验,React 的最佳 UI 框架(例如 Material UI 或其他)?

javascript - 为什么我们应该使用 import React from 'react'

javascript - 传统 bool 测试

c - 如何调用参数数量可变的函数?

javascript - Console.log 和函数返回 (Javascript)

c++ - 模拟静态/全局函数的最佳简单方法?

javascript - 在从 Phonegap 相机捕获的图像上绘制文本(自定义文本)

sql - "Execute does not exist"写入 Postgres 函数时

c - C 中的宏和函数冲突

javascript - react 绑定(bind)问题和内存管理