github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/utils/wait.go (about)

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  )
    10  
    11  // WAIT pauses the execution for a given period.
    12  // @param {Int | Float} timeout - Number value which indicates for how long to stop an execution.
    13  func Wait(ctx context.Context, args ...core.Value) (core.Value, error) {
    14  	err := core.ValidateArgs(args, 1, 1)
    15  
    16  	if err != nil {
    17  		return values.None, nil
    18  	}
    19  
    20  	arg := values.ToInt(args[0])
    21  
    22  	timer := time.NewTimer(time.Millisecond * time.Duration(arg))
    23  	select {
    24  	case <-ctx.Done():
    25  		timer.Stop()
    26  		return values.None, ctx.Err()
    27  	case <-timer.C:
    28  	}
    29  
    30  	return values.None, nil
    31  }