search
HomeWeChat AppletMini Program DevelopmentSolve the problem of code synchronization execution in small programs

When making small programs, do you often encounter these two synchronization problems:

1. When using a for loop, the operation in one loop has not yet ended, and the next loop has already started. If there is no interdependence between loops, the problem should not be big, but if the start of the next loop depends on the result of the previous loop, then there will be problems with this series of operations, such as drawing:

for (let index in images) {
      //每画一张图,都要在上一张图画结束才能开始,因为要计算画图位置
      ctx.drawImage
}

2 . Call the server interface to access data, download pictures, etc., but the server has not returned the data and the code has continued to execute other codes. This will obviously cause problems.

wx.downloadFile({
      url: URL,
      success(wr) {
      //如果其他执行代码在success代码块里面,还能保证在成功获取数据后正常执行
      //如果下载功能是共用的,其他操作逻辑肯定就会抽离出来,这样就保证不了同步执行了。
      }
});

How to solve it?
In the first case, many solutions on the Internet are to add sync or await, and some add setInterval. I did not choose any of these solutions, and used nested calls.

/**
   * 处理图片
   */
  handleOneImage: function(ctx, images, idx) {
    let that = this;
    let oneImage = images[idx];
    let pro = new Promise(function(resolve, reject) {
      if (oneImage == undefined) {
        //画图结束
        //执行一系列操作
      } else {
        //成功画图结束,执行下一张图的操作
        that.drawOneImage(ctx, oneImage, that.data.xp).then(isSuccess => {
          if (isSuccess == 'success') {
            that.handleOneImage(ctx, images, idx + 1);
          }
        });
      }
    });
    return pro;
  },
  /**
   * 画图片
   */
  drawOneImage: function(ctx, image, xp) {
    let that = this;
    //保证获取图片信息、画图等操作同步进行结束再返回结果
    let pro = new Promise(function(resolve, reject) {
      wx.getImageInfo({
        src: image,
        success: function(imageInfo) {
          let iWidth = imageInfo.width;
          let iHeight = imageInfo.height;
          let dWidth = (iWidth * 580) / iHeight;
          ctx.drawImage(image, xp, 0, dWidth, 580);
          ctx.stroke();
          that.setData({
            xp: that.data.xp + dWidth
          });
          resolve('success');
        }
      });
    });
    return pro;
  },

Second case: In fact, the code to solve the first case is also used to solve the second case. Using Promise, you can study the above code if necessary.

These solutions are also code snippets from a small program of mine. Xiaocheng’s name is Tu Zuo Yao. It is a small and beautiful small program for image synthesis and cutting. Everyone is welcome to try it.

Recommended tutorial: "WeChat Mini Program"

The above is the detailed content of Solve the problem of code synchronization execution in small programs. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:简书. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software