<commit_before>// +build ignore

package main

import (
	"encoding/binary"
	"fmt"
	"net/http"
	"os"
	"sync"
)

func panicIfError(err error) {
	if err != nil {
		fmt.Println("Fatal error ", err.Error())
		os.Exit(1)
	}
}

type Promise interface {
	Then(func(value interface{}) Promise) Promise
}

type AsyncRequest struct {
	Promise
	body int64
}

func (aReq *AsyncRequest) Then(chain func(value interface{}) Promise) Promise {
	return chain(aReq.body)
}

func (aReq *AsyncRequest) Get(path string) Promise {
	wait := make(chan int64)
	go func() {
		res, err := http.Get(path)
		defer res.Body.Close()
		panicIfError(err)
		var answer int64
		err = binary.Read(res.Body, binary.LittleEndian, &answer)
		panicIfError(err)
		wait <- answer
	}()
	aReq.body = <-wait
	return aReq
}

func main() {
	req := &AsyncRequest{}
	var wg sync.WaitGroup
	for i := 1; i <= 100; i++ {
		wg.Add(1)
		go func(r *AsyncRequest, iter int) {
			fmt.Println("Asynchronous Gets")
			r.Get("http://localhost:3000/odd").Then(func(response interface{}) Promise {
				fmt.Fprintf(os.Stdout, "(ODD) From Promise %d value => %v\n", iter, response)
				req2 := &AsyncRequest{}
				return req2.Get("http://localhost:3000/pair").Then(func(response interface{}) Promise {
					fmt.Fprintf(os.Stdout, "(PAIR) From Innert Promise %d value => %v\n", iter, response)
					wg.Done()
					return nil
				})
			})
		}(req, i)
	}
	wg.Wait()
}<commit_msg>Do not block promise, let it run and sen it's answer over channels<commit_after>// +build ignore

package main

import (
	"encoding/binary"
	"fmt"
	"net/http"
	"os"
	"sync"
)

func panicIfError(err error) {
	if err != nil {
		fmt.Println("Fatal error ", err.Error())
		os.Exit(1)
	}
}

type Promise interface {
	Then(func(value interface{}) Promise) Promise
}

type AsyncRequest struct {
	Promise
	body chan int64
}

func NewAsyncRequest() *AsyncRequest {
	return &AsyncRequest{
		body: make(chan int64),
	}
}

func (aReq *AsyncRequest) Then(chain func(value interface{}) Promise) Promise {
	return chain(<-aReq.body)
}

func (aReq *AsyncRequest) Get(path string) Promise {
	go func() {
		res, err := http.Get(path)
		defer res.Body.Close()
		panicIfError(err)
		var answer int64
		err = binary.Read(res.Body, binary.LittleEndian, &answer)
		panicIfError(err)
		aReq.body <- answer
	}()
	return aReq
}

func main() {
	req := NewAsyncRequest()
	var wg sync.WaitGroup
	for i := 1; i <= 100; i++ {
		wg.Add(1)
		go func(r *AsyncRequest, iter int) {
			fmt.Println("Asynchronous Gets")
			r.Get("http://localhost:3000/odd").Then(func(response interface{}) Promise {
				fmt.Fprintf(os.Stdout, "(ODD) From Promise %d value => %v\n", iter, response)
				req2 := NewAsyncRequest()
				return req2.Get("http://localhost:3000/pair").Then(func(response interface{}) Promise {
					fmt.Fprintf(os.Stdout, "(PAIR) From Innert Promise %d value => %v\n", iter, response)
					wg.Done()
					return nil
				})
			})
		}(req, i)
	}
	wg.Wait()
}
{'fraction_non_alphanumeric': 0.15753657706702962, 'fraction_numerical': 0.01497107859816264, 'mean_word_length': 3.764991896272285, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2697964', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1062, 'n_words': 276}