异步函数在调用时返回未定义

时间:2020-12-22 19:39:41

标签: javascript reactjs async-await

我在 React 项目中使用此功能时遇到了一些问题

import java.util.Random;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Student extends Thread{
    private Classroom classroomList[];
    static volatile int sitting = 0;
    
    
    volatile Lock lock = new ReentrantLock();
    
    

    public Student(Classroom[] classroomList) {
        this.classroomList = classroomList;
    }



    @Override
    public void run() {
        while(true) {
            int timeInRoom = (int) (Math.random() * 5000);

            Classroom classroom = classroomList[Classroom.classRoomNumber(classroomList.length)]; 
            if(!classroom.checkIfLecturerInRoomStudent()) {
                enter(classroom);
                
                
                
                if(classroom.checkIfLecturerInRoomStudent()) {
                    // Counting so all of the students can sit
                    while(sitting < classroom.numberOfStudentsInRoom()) {
                        sitting++;
                    }                   
                    //Here all students are sitting
                    sitDown(classroom);
                }
                
                
                
                
                sitting = 0;
                leave(classroom);
            }
            
            
                
        }
        
            
    }
    
    // Uses the Semaphore in the Classroom to control the students in the room
    // also it increments the number of current students
    synchronized void enter(Classroom classroom) {
        classroom.studentVisitorsSemaphoreEntered();
    }
    
    // Uses the Semaphore in the Classroom to control the students in the room
    // also it decrements the number of current students
    synchronized void leave(Classroom classroom) {
        classroom.studentVisitorsSemaphoreLeave();
    }
    
    // Uses the Semaphore in the Classroom to control the students in the room
    // also it decrements the number of current students
    synchronized void sitDown(Classroom classroom) {
        classroom.allSeated();
    }
            
}

当我调用它时,Console.log 返回 Undefined 但上面的控制台日志返回数据

let fetchData = async (event, imagen) => {
    const apiURL = `https://some_api_call/${imagen}`;
    await axios.get(apiURL).then((response) => {
      console.log(response.data.Imagen1.data);
      return response.data.Imagen1.data;
    });

2 个答案:

答案 0 :(得分:1)

let fetchData = async (event, imagen) => {
    const apiURL = `https://some_api_call/${imagen}`;
    return await axios.get(apiURL);
}
fetchData(event, rowData.Codigo).then(function (response) {
    console.log(response);
});

答案 1 :(得分:1)

您的 public class MidOfArray { static final int start = Integer.MAX_VALUE; static final int end = Integer.MAX_VALUE; public static void doesnotWork() { int mid = (start + end) / 2; System.out.println(mid); // output: -1 } public static void worksGreat() { int mid = start + ((end + start) / 2); System.out.println(mid); // output: 2147483646 } public static void main(String[] args) { doesnotWork(); worksGreat(); } } 函数没有 fetchData 语句。当有 return 时,您可能不想使用 then

await